USACO4.1.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
题解:很明显是个背包问题,这题主要问题就是怎么确定一个上界(木有想出怎么搞,好忧伤,只能去看题解囧)
1.如果出现包装盒的容量为1的情况,那么所有的数都能被表示。
2.如果所有的包装盒容量的最大公约数不为1(假设为g),那么可以组成的数肯定是g的倍数,任何不是g的倍数都不能被构成,所以解为无限大。
3.如果所有的包装盒容量的最大公约数为1,即互质,那么肯定存在一个最大的数不能被构造出来,但是怎么求出这个数呢?我就是卡在这了。。。无奈只好看DD牛的分析,他说:只需要根据“若i、j 互质,则关于x、y 的不定方程i*x+y*j=n必有正整数解,其中n>i*j”这一定理得出一个循环的上限
这样就解决了这个问题,不过这个定理怎么得出来的,完全不知道推导啊。。。。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* 2 ID:spcjv51 3 PROG:nuggets 4 LANG:C 5 */ 6 #include<stdio.h> 7 #include<stdlib.h> 8 #include<string.h> 9 #define MAXN 90000 10 int f[MAXN]; 11 int a[15]; 12 int n; 13 int gcd(int a,int b) 14 { 15 if(b==0)return a; 16 else 17 return gcd(b,a%b); 18 } 19 int main(void) 20 { 21 freopen("nuggets.in","r",stdin); 22 freopen("nuggets.out","w",stdout); 23 long i,j,ans,t,temp,flag; 24 memset(f,0,sizeof(f)); 25 scanf("%d",&n); 26 flag=0; 27 for(i=0;i<n;i++) 28 { 29 scanf("%d",&a[i]); 30 if(a[i]==1) 31 flag=1; 32 } 33 if(flag) 34 { 35 printf("0\n"); 36 return 0; 37 } 38 for(i=0;i<n-1;i++) 39 for(j=i+1;j<n;j++) 40 if(a[i]<a[j]) 41 { 42 temp=a[i]; 43 a[i]=a[j]; 44 a[j]=temp; 45 } 46 t=gcd(a[0],a[1]); 47 for(i=2;i<n;i++) 48 t=gcd(t,a[i]); 49 if(t!=1) 50 { 51 printf("0\n"); 52 return 0; 53 } 54 ans=a[0]*a[1]/gcd(a[0],a[1]); 55 f[0]=1; 56 for(i=0;i<n;i++) 57 for(j=a[i];j<=ans;j++) 58 if(f[j-a[i]]) 59 f[j]=1; 60 for(j=ans;j>=0;j--) 61 if(!f[j]) 62 { 63 printf("%ld\n",j); 64 break; 65 } 66 return 0; 67 }