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

版权声明:欢迎学习我的博客,希望ACM的发展越来越好~ https://blog.csdn.net/qq_41289920/article/details/88831718

题干:

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

扫描二维码关注公众号,回复: 5799751 查看本文章

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

猜你喜欢

转载自blog.csdn.net/qq_41289920/article/details/88831718
ZOJ