USACO 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

————————————————————————————这道题要想到一点就是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 }
 
时间: 2024-12-23 17:19:18

USACO 4.1 Beef McNuggets的相关文章

洛谷——P2737 [USACO4.1]麦香牛块Beef McNuggets

https://www.luogu.org/problemnew/show/P2737 题目描述 农夫布朗的奶牛们正在进行斗争,因为它们听说麦当劳正在考虑引进一种新产品:麦香牛块.奶牛们正在想尽一切办法让这种可怕的设想泡汤.奶牛们进行斗争的策略之一是“劣质的包装”.“看,”奶牛们说,“如果你只用一次能装3块.6块或者10块的三种包装盒包装麦香牛块,你就不可能满足一次只想买1.2.4.5.7.8.11.14或者17块麦香牛块的顾客了.劣质的包装意味着劣质的产品.” 你的任务是帮助这些奶牛.给出包装

[Luogu2737] [USACO4.1]麦香牛块Beef McNuggets

题目描述 农夫布朗的奶牛们正在进行斗争,因为它们听说麦当劳正在考虑引进一种新产品:麦香牛块.奶牛们正在想尽一切办法让这种可怕的设想泡汤.奶牛们进行斗争的策略之一是“劣质的包装”.“看,”奶牛们说,“如果你只用一次能装3块.6块或者10块的三种包装盒包装麦香牛块,你就不可能满足一次只想买1.2.4.5.7.8.11.14或者17块麦香牛块的顾客了.劣质的包装意味着劣质的产品.” 你的任务是帮助这些奶牛.给出包装盒的种类数N(1<=N<=10)和N个代表不同种类包装盒容纳麦香牛块个数的正整数(1&

P2737 [USACO4.1]麦香牛块Beef McNuggets

题目描述 农夫布朗的奶牛们正在进行斗争,因为它们听说麦当劳正在考虑引进一种新产品:麦香牛块.奶牛们正在想尽一切办法让这种可怕的设想泡汤.奶牛们进行斗争的策略之一是“劣质的包装”.“看,”奶牛们说,“如果你只用一次能装3块.6块或者10块的三种包装盒包装麦香牛块,你就不可能满足一次只想买1.2.4.5.7.8.11.14或者17块麦香牛块的顾客了.劣质的包装意味着劣质的产品.” 你的任务是帮助这些奶牛.给出包装盒的种类数N(1<=N<=10)和N个代表不同种类包装盒容纳麦香牛块个数的正整数(1&

【洛谷P2737】Beef McNuggets

首先有这样一个结论:若p,q为自然数,且gcd(p,q)=1,那么px+qy不能表示的最大数为pq-p-q 那么本题中p,q均取决于最大的两个数,不妨取256,那么上界为256^2-256*2 之后就是简单的完全背包 如果数据中有1,或者最大不能取到的数大于上界,就是0 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int N=256

P2737 [USACO4.1]麦香牛块Beef McNuggets(完全背包+数论确定上界)

题目链接:https://www.luogu.org/problem/show?pid=2737 题目大意:农夫布朗的奶牛们正在进行斗争,因为它们听说麦当劳正在考虑引进一种新产品:麦香牛块.奶牛们正在想尽一切办法让这种可怕的设想泡汤.奶牛们进行斗争的策略之一是“劣质的包装”.“看,”奶牛们说,“如果你只用一次能装3块.6块或者10块的三种包装盒包装麦香牛块,你就不可能满足一次只想买1.2.4.5.7.8.11.14或者17块麦香牛块的顾客了.劣质的包装意味着劣质的产品.” 你的任务是帮助这些奶牛

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY

【转】对于杭电OJ题目的分类

[好像博客园不能直接转载,所以我复制过来了..] 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDI

转载:hdu 题目分类 (侵删)

转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093. 1094.1095.1096.1097.1098.1106.1108.1157.116

COGS 696. [IOI1996][USACO 2.3] 最长前缀

★   输入文件:prefix.in   输出文件:prefix.out   简单对比时间限制:1 s   内存限制:128 MB 描述 USACO 2.3.1 IOI96 在生物学中,一些生物的结构是用包含其要素的大写字母序列来表示的.生物学家对于把长的序列分解成较短的序列(即元素)很感兴趣. 如果一个集合 P 中的元素可以通过串联(元素可以重复使用,相当于 Pascal 中的 “+” 运算符)组成一个序列 S ,那么我们认为序列 S 可以分解为 P 中的元素.元素不一定要全部出现(如下例中B