【ZOJ - 2955】Interesting Dart Game(背包,结论,裴蜀定理,数论)

题干:

Recently, Dearboy buys a dart for his dormitory, but neither Dearboy nor his roommate knows how to play it. So they decide to make a new rule in the dormitory, which goes as follows:

Given a number N, the person whose scores accumulate exactly to N by the fewest times wins the game.

Notice once the scores accumulate to more than N, one loses the game.

Now they want to know the fewest times to get the score N.

So the task is : 
Given all possible dart scores that a player can get one time and N, you are required to calculate the fewest times to get the exact score N.

Input

Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 50) which is the number of test cases. And it will be followed by T consecutive test cases.

Each test case begins with two positive integers M(the number of all possible dart scores that a player can get one time) and N.  Then the following M integers are the exact possible scores in the next line.

Notice: M (0 < M < 100), N (1 < N <= 1000000000), every possible score is (0, 100).

Output

For each test case, print out an integer representing the fewest times to get the exact score N.
If the score can't be reached, just print -1 in a line.

Sample Input

3
3 6
1 2 3
3 12
5 1 4
1 3
2

Sample Output

2
3
-1

题目大意:

  给m(<=100)个数,每个数的范围(1,100),一个n(<=1e9),问你能否通过组合这m个数来凑出n来。如果可以就输出最小步数,反之输出-1。

解题报告:

  n<=20000的时候可以直接背包,如果大于20000就先贪心最大的数,缩小下范围到20000以内,然后直接读表。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 20000 + 5;
const ll INF = 9999999999;
ll dp[MAX];
int a[MAX];
int main()
{int t;cin>>t;while(t--) {int m;ll n;scanf("%d%lld",&m,&n);for(int i = 0; i<MAX; i++) dp[i] = INF;for(int i = 1; i<=m; i++) scanf("%d",a+i);sort(a+1,a+m+1);dp[0] = 0;for(int i = 1; i<=m; i++) {for(int j = a[i]; j<=20000; j++) {dp[j] = min(dp[j],dp[j-a[i]] +1);}}ll qq = a[m];if(n<=20000) {if(dp[n] == INF) printf("-1\n");else printf("%lld\n",dp[n]);continue;}ll sub = (n-19000) / a[m];n -= sub * a[m];if(dp[n] == INF) printf("-1\n");else printf("%lld\n",sub + dp[n]);}return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440523.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

接口测试 java_接口测试--Java

1&#xff1a;interface :定义接口接口用于 模块与模块之间的连接 或者系统与系统间的连接2&#xff1a;软件系统UI层&#xff1a;程序的界面&#xff0c;主要为用户提供交互和操作--查看&#xff0c;输入&#xff0c;点击&#xff0c;等。业务逻辑层&#xff1a;进行复杂的业…

【ZOJ - 2949】Coins of Luck (概率dp,期望)

题干&#xff1a; 给你两种泡面 各N包&#xff0c;然后你扔硬币&#xff0c;正面朝上吃第一种&#xff0c;反面呢 吃第二种&#xff0c;有任意一种吃完时 就不需要抛硬币了&#xff0c;求停止抛硬币的期望。 Sample Input 2 1 2Sample Output 1.00 2.50 解题报告&#xff…

java perl_在Java中调用Perl脚本

有两种方法&#xff0c;第一种是直接 Runtime.getRuntime().exec("...");这里推荐第二种&#xff0c;可以得到输出和返回值&#xff1a;源代码如下&#xff1a;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;public c…

*【HDU - 6201】transaction transaction transaction(树形dp 或 spfa最长路 或 网络流)

题干&#xff1a; 题目大意&#xff1a; 给出一棵n个顶点的树&#xff0c;每个点有一个权值&#xff0c;代表商品的售价&#xff0c;树上每一条边上也有一个权值&#xff0c;代表从这条边经过所需要的花费。现在需要你在树上选择两个点&#xff0c;一个作为买入商品的点&#…

java 手写签名_Android 自定义View手写签名并保存图片

GIF压缩有问题&#xff0c;运行很顺滑&#xff01;&#xff01;&#xff01;1.自定义View——支持设置画笔颜色&#xff0c;画笔宽度&#xff0c;画板颜色&#xff0c;清除画板&#xff0c;检查是否有签名&#xff0c;保存画板图片(复制粘贴可直接使用)/*** Created by YyyyQ o…

【2019第十届蓝桥杯省赛C/C++B组题解】(非官方题解)

A。 数数题。 答案&#xff1a;490 B。 26进制模拟。 答案&#xff1a;BYQ C。 类似fib数列求值&#xff0c;递推一下就好。 答案&#xff1a;4659 D。 注意两个坑点&#xff0c;一个是正整数&#xff0c;所以枚举要从1开始。第二个坑点是互不相同的&#xff0c;为了达到这…

java对象模型 指令_深入理解多线程(二)—— Java的对象模型

上一篇文章中简单介绍过synchronized关键字的方式&#xff0c;其中&#xff0c;同步代码块使用monitorenter和monitorexit两个指令实现&#xff0c;同步方法使用ACC_SYNCHRONIZED标记符实现。后面几篇文章会从JVM源码的角度更加深入&#xff0c;层层剥开synchronized的面纱。在…

【POJ - 1651】Multiplication Puzzle(区间dp)

题干&#xff1a; The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one card out of the row and scores the number of points equal to the product of the number on the card taken …

java hive demo_java 操作hive通过jdbc

直接代码吧&#xff1a;记得要开启hive jdbc服务hive --service hiveserverpackage hive;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;public class HiveDemo {static{//注册jdbc驱动try {Class.forName(&…

【牛客 - 551D】CSL 的字符串(单调栈,思维)

题干&#xff1a; CSL 以前不会字符串算法&#xff0c;经过一年的训练&#xff0c;他还是不会……于是他打算向你求助。 给定一个字符串&#xff0c;只含有可打印字符&#xff0c;通过删除若干字符得到新字符串&#xff0c;新字符串必须满足两个条件&#xff1a; 原字符串中…

java 学生课程成绩_Java课设--学生成绩管理系统一

写在前面这个项目是Java课程的课设&#xff0c;一共花了5天的时间去完成它&#xff0c;在这期间感谢一些博主的帮助&#xff0c;让我了解到了一些新的技术知识&#xff0c;所以打算写这一系列博客来介绍一整个课设项目&#xff0c;也为了帮助之后的人&#xff0c;如有错误&…

【牛客 - 551E】CSL 的魔法(贪心,思维,STLmap,分块)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/551/E 来源&#xff1a;牛客网 有两个长度为 n 的序列&#xff0c;a0,a1,…,an−1a0,a1,…,an−1和 b0,b1,…,bn−1b0,b1,…,bn−1。CSL 有一种魔法&#xff0c;每执行一次魔法&#xff0c;可以任意挑…

java调用cplex案例_【CPLEX教程03】java调用cplex求解一个TSP问题模型

前面我们已经搭建好cplex的java环境了&#xff0c;相信大家已经跃跃欲试&#xff0c;想动手写几个模型了。今天就来拿一个TSP的问题模型来给大家演示一下吧~CPLEX系列教程可以关注我们的公众号哦&#xff01;获取更多精彩消息&#xff01;01 TSP寤烘ā关于TSP建模&#xff0c;就…

【牛客 - 551C】CSL 的密码(后缀数组,后缀自动机,随机算法)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/551/C 来源&#xff1a;牛客网 为了改变这一点&#xff0c;他决定重新设定一个密码。于是他随机生成了一个很长很长的字符串&#xff0c;并打算选择一个子串作为新密码。他认为安全的密码长度至少为…

java在一个类里实现存款_用Java编写一个简单的存款

package desposit.money;public class DespositMoney {public static void main(String[] args) {Customer c1 new Customer("第一个顾客",3);Customer c2 new Customer("第二个顾客",10);Customer c3 new Customer("第三个顾客",5);c1.start…

【牛客 - 551G】CSL的训练计划(二分 + 拓扑排序 + 优化卡常)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/551/G 来源&#xff1a;牛客网 题目描述 众所周知&#xff0c;CSL 是一个负责的集训队队长。为了让集训队的学弟们训练更加饱和&#xff0c;他根据每个人的能力&#xff0c;提出了 m 个题数要求。假如…

java 自动启动监听_Spring Boot 启动事件和监听器,太强大了!

大家都知道&#xff0c;在 Spring 框架中事件和监听无处不在&#xff0c;打通了 Spring 框架的任督二脉&#xff0c;事件和监听也是 Spring 框架必学的核心知识之一。一般来说&#xff0c;我们很少会使用到应用程序事件&#xff0c;但我们也不要忘了它们的存在&#xff0c;比如…

【Gym - 101196F】Removal Game (环形区间dp,环状,细节优化)

题干&#xff1a; 健健开发了一个游戏叫做<<者护守形隐>>&#xff0c;里面有一个情节是这样的&#xff0c;女主子纯藤武被坏人关在了密室里&#xff0c;作为男主的肖健当然要英雄救美。但是要打开密室的门&#xff0c;必须解开一道谜题。 门上有几个数字围成的一个…

java day_Java_Day7(上)

Java learning_Day7(上)内容常用类枚举类型常用类String 类java.lang.String 类代表不可变的字符序列。String 类的常见构造方法&#xff1a;String(String original)创建一个 String 对象为 original 的拷贝。String(char[] value)用一个字符数组创建一个 String 对象。String…

【FZU - 2202】犯罪嫌疑人(思维,假装建图,分类讨论)

题干&#xff1a; 福尔摩斯是个大侦探&#xff0c;他总是在解决疑难案件。这一次的案件也不例外&#xff0c;案件是这样的&#xff1a;有编号为1到N的N位嫌疑犯&#xff0c;他们其中有一个犯了罪&#xff0c;然后每个嫌疑犯都被询问,“哪一个人犯了罪&#xff1f;”犯罪嫌疑人…