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 之后就 ...
随机推荐
- Asp.net Identity 系列之 怎样修改Microsoft.AspNet.Identity.EntityFramework.IdentityUser 的 Id 字段的数据类型
这篇博客我们来学习如何将AspNetUsers 表的Id 字段 的类型由nvarchar(128) 改为Int 并且子增长 1.为什么要修改 如果你运行过 Asp.net mvc 示例项目,你好会发现 ...
- 如何通过js获取iframe框架中的内容
在父窗口中获取iframe中的元素 IE下:格式:window.frames["iframe的name值"].document.getElementById("ifram ...
- 【转】Android 系统菜单与自定义菜单
Android 系统菜单与自定义菜单实现方法如下:系统菜单显示DefaultMenu.java package com.wxz.menu; import com.wxz.menu.R; import ...
- NHProfiler使用方法
NHProfiler使用方法 NHProfiler是一个针对Nhibernate运行分析的工具. 使用如下: (1)在创建ISessionFactory的项目中引用NHProfiler安装目录下的Hi ...
- js实现多张图片同时放大缩小相对位置不变
项目要求需要用js实现同时放大多张图片相对位置不变,就和同事去一家国外网站的js文件中跟踪扒取了这一算法, 庆幸的是算法抠出来了并整理了出来,但遗憾的只知计算过程却弄不明白算法原理: 大体上是核心运算 ...
- wcf的binding和host
----------------------------Binding绑定:定义:绑定表示通讯信道的配置:定义了客户端与服务端之间的协议:---传输协议:http.tcp.命名管道.msmq,自定义( ...
- OpenGl绘制螺旋线
/** * 缓冲区工具类 */public class BufferUtil { /** * 将浮点数组转换成字节缓冲区 */ public static ByteBuffer arr2ByteB ...
- 网际协议:无连接数据报交付(IPv4)
一.数据报格式:
- Vijos1057 盖房子(DP经典题)
之前没有怎么刷过dp的题,所以在此学习了~(感谢walala大神的思路,给了我很大的启发) 也算是自己学习的另一种dp题型吧 先贴上状态转移方程: if(a[i][j]) f[i][j]=min(f[ ...
- iOS Web开发激活css的active伪类
最近在做一个资讯客户端,用到UIWebview展示一些网页内容,本来想做一个简单的按压效果,发现在css中设置active属性一直不管用. 查阅了一下资料,今天发现,要让css active伪类生效, ...