您的位置:首页 > 其它

USACO4.1.1--Beef McNuggets

2013-03-20 01:37 211 查看
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”这一定理得出一个循环的上限
这样就解决了这个问题,不过这个定理怎么得出来的,完全不知道推导啊。。。。

View Code
/*
ID:spcjv51
PROG:nuggets
LANG:C
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXN 90000
int f[MAXN];
int a[15];
int n;
int gcd(int a,int b)
{
if(b==0)return a;
else
return gcd(b,a%b);
}
int main(void)
{
freopen("nuggets.in","r",stdin);
freopen("nuggets.out","w",stdout);
long i,j,ans,t,temp,flag;
memset(f,0,sizeof(f));
scanf("%d",&n);
flag=0;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]==1)
flag=1;
}
if(flag)
{
printf("0\n");
return 0;
}
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
t=gcd(a[0],a[1]);
for(i=2;i<n;i++)
t=gcd(t,a[i]);
if(t!=1)
{
printf("0\n");
return 0;
}
ans=a[0]*a[1]/gcd(a[0],a[1]);
f[0]=1;
for(i=0;i<n;i++)
for(j=a[i];j<=ans;j++)
if(f[j-a[i]])
f[j]=1;
for(j=ans;j>=0;j--)
if(!f[j])
{
printf("%ld\n",j);
break;
}
return 0;
}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: