【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 ;
}

 

全部评论

相关推荐

07-22 11:12
门头沟学院 Java
不是,我就随手投的怎么还真发面试啊
皮格吉:大厂特别快的——来自已经被共享中
点赞 评论 收藏
分享
06-08 22:25
门头沟学院 Java
从零开始的转码生活:这hr不会打开手机不分青红皂白给所有人群发这句话,过一会再给所有人再发一遍,这肯定会有重复的,不管,再过一会再发一遍
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务