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. 33、Python.Unix和Linux系统管理指南.(美)基弗特

  2. 【工作笔记一】【转】Visual Studio 2012常用快捷键总结

    Visual Studio 2012常用快捷键总结 原文  http://blog.csdn.net/yl2isoft/article/details/9886379   写在前面: 都知道,合理使用 ...

  3. NET开发面向对象2

    面向对象 (2) 继续上一篇<ASP.NET开发,从二层至三层,至面向对象>http://www.cnblogs.com/insus/p/3822624.html .我们了解到怎样把自己的 ...

  4. MogileFS

    分布式文件系统 ~MogileFS~ 一.分布式文件系统 分布式文件系统(Distributed File System)是指文件系统管理的物理存储资源不一定直接连接在本地节点上,而是通过计算机网络与 ...

  5. SZU:D89 The Settlers of Catan

    Judge Info Memory Limit: 65536KB Case Time Limit: 3000MS Time Limit: 3000MS Judger: Number Only Judg ...

  6. vector如何进行局部排序

    对于vector的全体排序,我们知道sort(vv.begin(),vv.end())来进行的. 但是对于如果是局部排序的话,比如,vector有100个元素,但我只想对10-80之间的数进行排序,如 ...

  7. Jackson 格式化日期问题

    Jackson 默认是转成timestamps形式的,如何使用自己需要的类型, 解决办法: 1.在实体字段上使用@JsonFormat注解格式化日期 @JsonFormat(locale=" ...

  8. NSSortDescriptor(数组排序)

    如果数组里面的每一个元素都是一个个model,例如 DepartsDate.h文件 [plain] view plaincopy #import <Foundation/Foundation.h ...

  9. 【NET】Winform用户控件的初步封装之编辑控件

    编辑控件 public abstract partial class TEditorBase <TEntity, TRepository, TSqlStrConstruct> : User ...

  10. TaskTracker获取并执行map或reduce任务的过程1

    TaskTracker获取并执行map或reduce任务的过程(一) 我们知道TaskTracker在默认情况下,每个3秒就行JobTracker发送一个心跳包,也就是在这个心跳包中包含对任务的请求. ...