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就好了

 /*
ID: ivorysi
PROG: nuggets
LANG: C++
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <string.h>
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
#define inf 0x3f3f3f3f
#define MAXN 400005
#define ivorysi
#define mo 97797977
#define ha 974711
#define ba 47
#define fi first
#define se second
#define pii pair<int,int>
using namespace std;
typedef long long ll;
int num[],s,dp[];
int n,now,ans;
int gcd(int a,int b) {return b== ? a : gcd(b,a%b);}
void solve(){
scanf("%d",&n);
siji(i,,n) {
scanf("%d",&num[i]);
if(s==) s=num[i];
else s=gcd(s,num[i]);
}
if(s!=) {
puts("");exit();
}
sort(num+,num+n+);
dp[]=;
for(int i=;i<=;++i) {
siji(j,,n) {
if(i-num[j]>=) {
dp[i]=max(dp[i],dp[i-num[j]]);
}
else break;
}
if(dp[i]==) ans=i;
}
printf("%d\n",ans); }
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("nuggets.in","r",stdin);
freopen("nuggets.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
solve();
}
 

USACO 4.1 Beef McNuggets的更多相关文章

  1. USACO Beef McNuggets

    洛谷 P2737 [USACO4.1]麦香牛块Beef McNuggets https://www.luogu.org/problem/P2737 JDOJ 1813: Beef McNuggets ...

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

    P2737 [USACO4.1]麦香牛块Beef McNuggets 13通过 21提交 题目提供者该用户不存在 标签USACO 难度普及+/提高 提交  讨论  题解 最新讨论 暂时没有讨论 题目描 ...

  3. USACO 4.1.1 麦香牛块 Beef McNuggets

    题目大意 给你\(n\)个数\(a_1, a_2 ... a_n\), 要你求最大的正整数\(m\)使得方程\(a_1 x_1 + a_2 x_2 + ... + a_n x_n = m\)无非负整数 ...

  4. 洛谷 P2737 [USACO4.1]麦香牛块Beef McNuggets Label:一点点数论 && 背包

    题目描述 农夫布朗的奶牛们正在进行斗争,因为它们听说麦当劳正在考虑引进一种新产品:麦香牛块.奶牛们正在想尽一切办法让这种可怕的设想泡汤.奶牛们进行斗争的策略之一是“劣质的包装”.“看,”奶牛们说,“如 ...

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

    https://www.luogu.org/problemnew/show/P2737 题目描述 农夫布朗的奶牛们正在进行斗争,因为它们听说麦当劳正在考虑引进一种新产品:麦香牛块.奶牛们正在想尽一切办 ...

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

    题目描述 农夫布朗的奶牛们正在进行斗争,因为它们听说麦当劳正在考虑引进一种新产品:麦香牛块.奶牛们正在想尽一切办法让这种可怕的设想泡汤.奶牛们进行斗争的策略之一是“劣质的包装”.“看,”奶牛们说,“如 ...

  7. USACO4.1 Beef McNuggets【数学/结论】

    吐槽/心路历程 打开这道题的时候:*&@#%*#?!这不是小凯的疑惑吗?好像还是个加强版的?我疑惑了.原来$USACO$才是真的强,不知道什么时候随随便便就押中了题目. 对于我这种蒟蒻来说,这 ...

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

    题目描述 农夫布朗的奶牛们正在进行斗争,因为它们听说麦当劳正在考虑引进一种新产品:麦香牛块.奶牛们正在想尽一切办法让这种可怕的设想泡汤.奶牛们进行斗争的策略之一是“劣质的包装”.“看,”奶牛们说,“如 ...

  9. 【洛谷P2737】Beef McNuggets

    首先有这样一个结论:若p,q为自然数,且gcd(p,q)=1,那么px+qy不能表示的最大数为pq-p-q 那么本题中p,q均取决于最大的两个数,不妨取256,那么上界为256^2-256*2 之后就 ...

随机推荐

  1. HDOJ 5063 Operation the Sequence

    注意到查询次数不超过50次,那么能够从查询位置逆回去操作,就能够发现它在最初序列的位置,再逆回去就可以求得当前查询的值,对于一组数据复杂度约为O(50*n). Operation the Sequen ...

  2. jQuery MiniUI

    今天找到一个关于jQuery的又一个不错的UI,特此保存下.备用. http://www.miniui.com/

  3. idea中output log4j中文乱码

    1.设置tomcat中的VM optins:-Dfile.encofing=UTF-8 2.idea安装目录bin文件夹中idea.exe.vmoptions.idea64.exe.vmoptions ...

  4. js urlencode , encodeURIComponent

    js 对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent ...

  5. Jumony快速抓取网页

    Jumony快速抓取网页 --- Jumony使用笔记--icode   作者:郝喜路   个人主页:http://www.cnicode.com      博客地址:http://haoxilu.c ...

  6. Orchard Logging

    Orchard 刨析:Logging 最近事情比较多,有预研的,有目前正在研发的,都是很需要时间的工作,所以导致这周只写了两篇Orchard系列的文章,这边不能保证后期会很频繁的更新该系列,但我会写完 ...

  7. python chanllenge题解

    网址:chanllenge 修改url最后的html的前缀为答案,就可以过关. 页面上很多只有一幅图片,实际上题目描述全在页面源码中. 然后推荐一个在线代码运行的网站 ideone 查看所有源码:ht ...

  8. USBWebserver v8.6 PHP环境

    USBWebserver v8.6 Welcome on usbwebserver.com/net/eu USBWebserver v8.6 New in this version New langu ...

  9. android中自定义shape

    <shape> <!-- 实心 --> <solid android:color="#ff9d77"/> <!-- 渐变 --> & ...

  10. Java字符串转换为日期和时间比较大小

    字符串转换为时间: String data = "2014/7/11"; SimpleDateFormat dfs = new SimpleDateFormat("yyy ...