USACO Section 4.1 Beef McNuggets(数论 & 背包)

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

题意:使用 n 个数,输出一个最大不能组成的数,如果不存在输出0。
分析:第一步:判断这 n 个数的gcd,如果 gcd!= 1 则无解;
第二步:如果 gcd=1 则解小于最大的两个数的LCM。
第三步:背包,使用标记数组 d[],d[i]=d[i-a[j]];
简单证明一下:
第一步:
a1*x1+a2*x2+……+an*xn = p;
a1*y1+a2*y2+……+an*yn = p+1;
所以:a1*(y1-x1)+a2*(y2-x2)+……+an*(yn-xn) = 1;
所以:gcd(a1,a2,……,an)*[a1/gcd(a1,a2,……,an)*(y1-x1) + a2/gcd(a1,a2,……,an)*(y2-x2) +……+ an/gcd(a1,a2,……,an)*(yn-xn)]=1
所以:gcd(a1,a2,……,an)=1;
第二步:……

View Code
/*
  ID: dizzy_l1
  LANG: C++
  TASK: nuggets
*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#define MAXN 11
#define MAXV 1000000
using namespace std;

int a[MAXN];
bool d[MAXV];

int gcd(int A,int B)
{
    if(A==0) return B;
    return gcd(B%A,A);
}

int main()
{
    freopen("nuggets.in","r",stdin);
    freopen("nuggets.out","w",stdout);
    int n,sumv,i,j,t;
    while(scanf("%d",&n)==1)
    {
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        if(n==1)
        {
            printf("0\n");
            continue;
        }
        sort(a,a+n);
        t=a[0];
        for(i=1;i<n;i++)
        {
            t=gcd(t,a[i]);
            if(t==1) break;
        }
        if(t!=1)
        {
            printf("0\n");
            continue;
        }
        memset(d,false,sizeof(d));
        d[0]=true;
        sumv=a[n-2]*a[n-1]*gcd(a[n-2],a[n-1]);
        for(i=0;i<n;i++)
        {
            for(j=a[i];j<=sumv;j++)
            {
                if(d[j-a[i]]) d[j]=true;
            }
        }
        for(i=sumv;i>0;i--)
        {
            if(d[i]==false)
            {
                break;
            }
        }
        printf("%d\n",i);
    }
    return 0;
}

你可能感兴趣的:(USACO Section 4.1 Beef McNuggets(数论 & 背包))