This is a explanation of this problem from USACO's training website. I have converted it to markdown. Please do not just copy code; you will not learn anything; at least type it out and understand so you can do it yourself in the future!

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.

### 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
``````

Java

C++

Pascal

## ANALYSIS

Hal Burch

This problem is fairly straight-forward dynamic programming. We know that a value X is possible if and only if X - vi is possible, where vi is the number of nuggets in one of the package types.

The only way for there to be no bound to the largest number which is unobtainable is if the greatest common divisor of the package sizes is greater than 1, so first check for that.

Otherwise, go through the sizes in increasing order. For each impossible value, update the largest number found thus far. Otherwise, if X is possible, mark X + vi for each i as being possible. Whenever the last 256 of the sizes have all been possible, you know that all the sizes from here on out are also possible (you actually only need the last min {vi} to be possible, but doing the extra 256 steps takes almost no time).

``````#include <stdio.h>

/* the number and value of the package sizes */
int nsize;
int sizes;

/* cando specifies whether a given number is possible or not */
/* since max size = 256, we'll never need to mark more than 256
in the future, so we use a sliding window */
int cando;

int gcd(int a, int b)
{ /* uses standard gcd algorithm to computer greatest common divisor */
int t;

while (b != 0)
{
t = a % b;
a = b;
b = t;
}
return a;
}

int main(int argc, char **argv)
{
FILE *fout, *fin;
int lv, lv2; /* loop variable */
int pos;     /* count position */
int last;    /* last impossible count */

if ((fin = fopen("nuggets.in", "r")) == NULL)
{
perror ("fopen fin");
exit(1);
}
if ((fout = fopen("nuggets.out", "w")) == NULL)
{
perror ("fopen fout");
exit(1);
}

fscanf (fin, "%d", &nsize);
for (lv = 0; lv < nsize; lv++) fscanf (fin, "%d", &sizes[lv]);

/* ensure gcd = 1 */
lv2 = sizes;
for (lv = 1; lv < nsize; lv++)
lv2 = gcd(sizes[lv], lv2);
if (lv2 != 1)
{ /* gcd != 1, no bound on size that cannot be purchased */
fprintf (fout, "0\n");
return 0;
}

/* we can do 0 */
cando = 1;

lv = pos = 0;
last = 0;
while (pos < 2000000000)
{ /* bound as stated */

/* if last 256 were all possible, we are done */
if (pos - last > 256) break;

if (!cando[lv])
last = pos; /* this isn't possible, update last impossible */
else
{ /* this is possible */
cando[lv] = 0; /* mark pos+256 as impossible */

/* mark pos + size as possible for each package size */
for (lv2 = 0; lv2 < nsize; lv2++)
cando[(lv+sizes[lv2])%256] = 1;
}

/* update lv & pos */
lv = (++pos) % 256;
}
if (pos >= 2000000000) last = 0; /* shouldn't occur */

fprintf (fout, "%i\n", last);
return 0;
}
``````

Alex Schwendner

Given two relatively prime numbers N and M, the largest number that you cannot make is NM - M - N, that is, the product minus the sum. We do not have two numbers; however, even if we were using only two of them the answer could not exceed 256 * 256 - 256 - 256 = 65024 (much less then the 2,000,000,000 that we were guaranteed). It is therefore reasonable to have an array of 65024 booleans and work on all of them (If cando[x] then cando[x + sizes[p]]). If there is some number above 65024 that cannot be made then we know that there is no bound to the largest number. Because cando is set to false, if every number can be made then the program will output ‘0’ automatically. This program is shorter and easier to code, and although it is somewhat less efficient, it is easily able to solve the problem in the time limit.

``````#include <fstream.h>
#include <string.h>

int
main ()
{

int     n;
int     sizes;

ifstream filein ("nuggets.in");
filein >> n;
for (int in = 0; in < n; ++in) {
filein >> sizes[in];
}
filein.close ();

bool    cando;
memset (cando, 0, 67000);

for (int loop = 0; loop < n; ++loop) {
cando[sizes[loop]] = true;
for (int loop2 = 0; loop2 < 66700; ++loop2) {
if (cando[loop2]) {
cando[loop2 + sizes[loop]] = true;
}
}
}

ofstream fileout ("nuggets.out");
for (int out = 66500; out >= 0; --out) {
if (!cando[out]) {
if (out < 66000) {
fileout << out << endl;
break;
}
else {
fileout << 0 << endl;
break;
}
}
}
fileout.close ();

return (0);
}
``````