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就好了
/*
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的更多相关文章
- USACO Beef McNuggets
洛谷 P2737 [USACO4.1]麦香牛块Beef McNuggets https://www.luogu.org/problem/P2737 JDOJ 1813: Beef McNuggets ...
- 洛谷P2737 [USACO4.1]麦香牛块Beef McNuggets
P2737 [USACO4.1]麦香牛块Beef McNuggets 13通过 21提交 题目提供者该用户不存在 标签USACO 难度普及+/提高 提交 讨论 题解 最新讨论 暂时没有讨论 题目描 ...
- 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\)无非负整数 ...
- 洛谷 P2737 [USACO4.1]麦香牛块Beef McNuggets Label:一点点数论 && 背包
题目描述 农夫布朗的奶牛们正在进行斗争,因为它们听说麦当劳正在考虑引进一种新产品:麦香牛块.奶牛们正在想尽一切办法让这种可怕的设想泡汤.奶牛们进行斗争的策略之一是“劣质的包装”.“看,”奶牛们说,“如 ...
- 洛谷——P2737 [USACO4.1]麦香牛块Beef McNuggets
https://www.luogu.org/problemnew/show/P2737 题目描述 农夫布朗的奶牛们正在进行斗争,因为它们听说麦当劳正在考虑引进一种新产品:麦香牛块.奶牛们正在想尽一切办 ...
- [Luogu2737] [USACO4.1]麦香牛块Beef McNuggets
题目描述 农夫布朗的奶牛们正在进行斗争,因为它们听说麦当劳正在考虑引进一种新产品:麦香牛块.奶牛们正在想尽一切办法让这种可怕的设想泡汤.奶牛们进行斗争的策略之一是“劣质的包装”.“看,”奶牛们说,“如 ...
- USACO4.1 Beef McNuggets【数学/结论】
吐槽/心路历程 打开这道题的时候:*&@#%*#?!这不是小凯的疑惑吗?好像还是个加强版的?我疑惑了.原来$USACO$才是真的强,不知道什么时候随随便便就押中了题目. 对于我这种蒟蒻来说,这 ...
- P2737 [USACO4.1]麦香牛块Beef McNuggets
题目描述 农夫布朗的奶牛们正在进行斗争,因为它们听说麦当劳正在考虑引进一种新产品:麦香牛块.奶牛们正在想尽一切办法让这种可怕的设想泡汤.奶牛们进行斗争的策略之一是“劣质的包装”.“看,”奶牛们说,“如 ...
- 【洛谷P2737】Beef McNuggets
首先有这样一个结论:若p,q为自然数,且gcd(p,q)=1,那么px+qy不能表示的最大数为pq-p-q 那么本题中p,q均取决于最大的两个数,不妨取256,那么上界为256^2-256*2 之后就 ...
随机推荐
- C#编程实践—EventBroker简单实现
前言 话说EventBroker这玩意已经不是什么新鲜货了,记得第一次接触这玩意是在进第二家公司的时候,公司产品基础架构层中集成了分布式消息中间件,在.net基础服务层中使用EventBroker的模 ...
- 由浅入深学习.NET CLR 系列:目录
经过对Android的一阵折腾,些许熟悉了一些Java的东东,又开始转战.NET.我觉得学习最好和工作不要相离太远,才会更加随笔随意,索性整理一些比较系统的.NET的基础知识学习学习.一提起学习.NE ...
- DDD实践2
DDD实践切入点(二) 承前:大型系统的支撑,应用系统开发思想的变迁,DDD实践切入点(一) 从大比例结构入手已经开始了系统的建设,大家都知道需求是会不断变化不断深入的,刚开始自然是模糊的大比例结构对 ...
- Ninject 在 Winform、 Asp.net MVC中连络EntityFramework的应用
Ninject 在 Winform. Asp.net MVC中连络EntityFramework的应用( 注入同一个DbContext的应用名人名言:科学是老老实实的东西,它要靠许许多多人民的劳动和智 ...
- springmvc3.1.1+hibernate4
上篇介绍了基本的配置,这篇着重介绍与hibernate4整合. 1.web.xml文件中加入spring-hibernate的配置.新的web.xml文件内容如下: <?xml version= ...
- lex与yacc快速入门
lex与yacc快速入门 [原创] 声明:原创文章,转载注明出处http://www.cnblogs.com/lucasysfeng/ 联系作者:lucasysfeng@gmail.com 第一节.l ...
- JS实现等比例缩放图片
JS实现等比例缩放图片 2014-01-19 21:57 by 龙恩0707, 40 阅读, 0 评论, 收藏, 编辑 JS实现等比例缩放图片 有时候我们前端页面只有500×500像素的宽和高的布局, ...
- ASP添加进度条
今日在学习JavaScript所有写个通用的进度条,防止网页假死.让用户更清楚地知道此网页正在进行加载或者处理一些事情,所有加载进度条是一个网站的必要性. 在网页中Page_load首先要加载此进度条 ...
- ACM STUDY
ACM学习<二> 穷举算法思想: 一句话:就是从所有可能的情况,搜索出正确的答案. 步骤: 1.对于一种可能的情况,计算其结果. 2.判断结果是否满足,YES计 ...
- SharePoint开发
做SharePoint开发有时候是一件比较痛苦的事情,毕竟庞大的框架总是笨重的~~ 往往如果采取传统的方式开发SharePoint的话,更改一个代码需要有以下操作: 1)更改代码 2)VS编译——&g ...