USACO 4.1 Beef McNuggets
Posted sigongzi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了USACO 4.1 Beef McNuggets相关的知识,希望对你有一定的参考价值。
Hubert Chen
Farmer Brown‘s cows are up in arms, having heard that McDonalds is considering the introduction of a new product: Beef McNuggets. The cows are trying to find any possible way to put such a product in a negative light.
One strategy the cows are pursuing is that of `inferior packaging‘. ``Look,‘‘ say the cows, ``if you have Beef McNuggets in boxes of 3, 6, and 10, you can not satisfy a customer who wants 1, 2, 4, 5, 7, 8, 11, 14, or 17 McNuggets. Bad packaging: bad product.‘‘
Help the cows. Given N (the number of packaging options, 1 <= N <= 10), and a set of N positive integers (1 <= i <= 256) that represent the number of nuggets in the various packages, output the largest number of nuggets that can not be purchased by buying nuggets in the given sizes. Print 0 if all possible purchases can be made or if there is no bound to the largest number.
The largest impossible number (if it exists) will be no larger than 2,000,000,000.
PROGRAM NAME: nuggets
INPUT FORMAT
Line 1: | N, the number of packaging options |
Line 2..N+1: | The number of nuggets in one kind of box |
SAMPLE INPUT (file nuggets.in)
3 3 6 10
OUTPUT FORMAT
The output file should contain a single line containing a single integer that represents the largest number of nuggets that can not be represented or 0 if all possible purchases can be made or if there is no bound to the largest number.
SAMPLE OUTPUT (file nuggets.out)
17
————————————————————————————
这道题要想到一点就是ax+by=gcd(a,b)
Print 0 if all possible purchases can be made or if there is no bound to the largest number.
The largest impossible number (if it exists) will be no larger than 2,000,000,000.
这句话,输出0如果所有的方案都可以满足或者不存在最大数的上限,最大的可能数如果存在不超过2,000,000,000。
请记住红字,并且不要被范围吓到,因为范围要自己推出来
首先的首先,我们发现如果所有数的gcd不等于1,那么肯定有的数无法得到,所以此时是0。
然后开始讨论gcd是1时的范围
ax+by=1;|by|-|ax|=1;那么我们得到一个|ax|和一个|by|,他们差值1,得到长度为2的一个可取数范围,我们用他们自身再次累加,会发现得到长度为3的可取数范围
当这个范围==最小的包装内牛块,我们就发现后面的数都可以得到了
然后这个范围最大也就是256*256,不是20亿啦……【谁会一次性买20亿牛块啊喂……】
所以复杂度是O(256*256*10)然后直接dp就好了
1 /* 2 ID: ivorysi 3 PROG: nuggets 4 LANG: C++ 5 */ 6 #include <iostream> 7 #include <cstdio> 8 #include <cstring> 9 #include <algorithm> 10 #include <queue> 11 #include <set> 12 #include <vector> 13 #include <string.h> 14 #define siji(i,x,y) for(int i=(x);i<=(y);++i) 15 #define gongzi(j,x,y) for(int j=(x);j>=(y);--j) 16 #define xiaosiji(i,x,y) for(int i=(x);i<(y);++i) 17 #define sigongzi(j,x,y) for(int j=(x);j>(y);--j) 18 #define inf 0x3f3f3f3f 19 #define MAXN 400005 20 #define ivorysi 21 #define mo 97797977 22 #define ha 974711 23 #define ba 47 24 #define fi first 25 #define se second 26 #define pii pair<int,int> 27 using namespace std; 28 typedef long long ll; 29 int num[15],s,dp[70005]; 30 int n,now,ans; 31 int gcd(int a,int b) {return b==0 ? a : gcd(b,a%b);} 32 void solve(){ 33 scanf("%d",&n); 34 siji(i,1,n) { 35 scanf("%d",&num[i]); 36 if(s==0) s=num[i]; 37 else s=gcd(s,num[i]); 38 } 39 if(s!=1) { 40 puts("0");exit(0); 41 } 42 sort(num+1,num+n+1); 43 dp[0]=1; 44 for(int i=1;i<=33000;++i) { 45 siji(j,1,n) { 46 if(i-num[j]>=0) { 47 dp[i]=max(dp[i],dp[i-num[j]]); 48 } 49 else break; 50 } 51 if(dp[i]==0) ans=i; 52 } 53 printf("%d\n",ans); 54 55 } 56 int main(int argc, char const *argv[]) 57 { 58 #ifdef ivorysi 59 freopen("nuggets.in","r",stdin); 60 freopen("nuggets.out","w",stdout); 61 #else 62 freopen("f1.in","r",stdin); 63 #endif 64 solve(); 65 }
以上是关于USACO 4.1 Beef McNuggets的主要内容,如果未能解决你的问题,请参考以下文章
洛谷——P2737 [USACO4.1]麦香牛块Beef McNuggets
[Luogu2737] [USACO4.1]麦香牛块Beef McNuggets
P2737 [USACO4.1]麦香牛块Beef McNuggets