USACO section 4.1 Beef McNuggets(数论+背包)

从零开始:用摩尔线程S4000运行开源AI项目的完整流程 本文详细介绍了如何使用摩尔线程S4000 GPU从零开始运行开源AI项目的完整流程,包括硬件选择、驱动安装、容器化环境配置以及典型AI项目的实战部署。通过性能优化技巧,显著提升推理速度,适合开发者快速上手国产GPU的AI应用开发。 阅读详情

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

思路:有个结论,有两个数p,q,且gcd(q,p)=1,则最大无法表示成px+qy(x>=0,y>=0)的数是pq-q-p(对于n>pq-q-p,都可以表示成px+qy;而pq-q-p,就无法表示成px+qy)。所以我们只需考虑小于pq-q-p的范围的最小值。对于一些无解的(全体最大公约数>1),或无数解的(有一个‘1’),应提前判断。 其实我们可以干脆全取上界为256*256-256*2。直接进行背包就可以了。


/*
ID:nealgav1
LANG:C++
PROG:nuggets
*/
#include<fstream>
#include<cstdio>
#include<cstring>
using namespace std;
ifstream cin("nuggets.in");
ofstream cout("nuggets.out");
const int mm=266;
int f[mm];
int dp[mm*mm+3];
int gcd(int a,int b)
{ int z;
  while(a)
  {
    z=a;a=b%a;b=z;
  }
  return b;
}
void Min(int&x,const int y)
{
  if(x>y)x=y;
}
int main()
{ int n;
  while(cin>>n)
  { bool flag=0;
    for(int i=0;i<n;++i)
      {cin>>f[i];if(f[i]==1)flag=1;}
    int z=gcd(f[0],f[1]);
    for(int i=1;i<n;++i)
      z=gcd(z,f[i]);
    if(z>1)flag=1;
    int pos=mm*mm;
    for(int i=0;i<n;++i)
      for(int j=0;j<n;++j)
      if(i^j&&gcd(f[i],f[j])==1)
      Min(pos,f[i]*f[j]-f[i]-f[j]);
    memset(dp,0,sizeof(dp));
    int ans=0;dp[0]=1;
    z=mm*mm;
    for(int i=0;i<z;++i)
      if(dp[i])
      { for(int j=0;j<n;++j)
        if(i+f[j]<z)
        dp[i+f[j]]=1;
      }else if(ans<i)ans=i;
      if(ans>pos||ans>256*256)ans=0;
  ///  for(int i=0;i<=pos;++i)
   ///   cout<<i<<" "<<dp[i]<<endl;
    ///if(flag)ans=0;
    cout<<ans<<"\n";
  }
}



USER: Neal Gavin Gavin [nealgav1]
TASK: nuggets
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 3632 KB]
   Test 2: TEST OK [0.000 secs, 3632 KB]
   Test 3: TEST OK [0.000 secs, 3632 KB]
   Test 4: TEST OK [0.000 secs, 3632 KB]
   Test 5: TEST OK [0.000 secs, 3632 KB]
   Test 6: TEST OK [0.000 secs, 3632 KB]
   Test 7: TEST OK [0.011 secs, 3632 KB]

All tests OK.

Your program ('nuggets') produced all correct answers! This is your submission #3 for this problem. Congratulations!

Here are the test data inputs:

------- test 1 ----
3
3
6
10
------- test 2 ----
2
2
3
------- test 3 ----
1
1
------- test 4 ----
4
252
250
254
256
------- test 5 ----
2
255
254
------- test 6 ----
5
251
252
250
254
256
------- test 7 ----
10
238
240
242
244
246
248
250
252
254
255
Beef McNuggets
Hal Burch

This problem is fairly straight-forward dynamic programming. We know that a value X is possible if and only if X - vi is possible, where vi is the number of nuggets in one of the package types.

The only way for there to be no bound to the largest number which is unobtainable is if the greatest common divisor of the package sizes is greater than 1, so first check for that.

Otherwise, go through the sizes in increasing order. For each impossible value, update the largest number found thus far. Otherwise, if X is possible, mark X + vi for each i as being possible. Whenever the last 256 of the sizes have all been possible, you know that all the sizes from here on out are also possible (you actually only need the last min {vi} to be possible, but doing the extra 256 steps takes almost no time).

#include <stdio.h>

/* the number and value of the package sizes */
int nsize;
int sizes[10];

/* cando specifies whether a given number is possible or not */
/* since max size = 256, we'll never need to mark more than 256
   in the future, so we use a sliding window */
int cando[256];

int gcd(int a, int b)
 { /* uses standard gcd algorithm to computer greatest common divisor */
  int t;

  while (b != 0)
   {
    t = a % b;
    a = b;
    b = t;
   }
  return a;
 }

int main(int argc, char **argv)
 {
  FILE *fout, *fin;
  int lv, lv2; /* loop variable */
  int pos;     /* count position */
  int last;    /* last impossible count */

  if ((fin = fopen("nuggets.in", "r")) == NULL)
   {
    perror ("fopen fin");
    exit(1);
   }
  if ((fout = fopen("nuggets.out", "w")) == NULL)
   {
    perror ("fopen fout");
    exit(1);
   }

  /* read in data */
  fscanf (fin, "%d", &nsize);
  for (lv = 0; lv < nsize; lv++) fscanf (fin, "%d", &sizes[lv]);

  /* ensure gcd = 1 */
  lv2 = sizes[0];
  for (lv = 1; lv < nsize; lv++)
    lv2 = gcd(sizes[lv], lv2);
  if (lv2 != 1)
   { /* gcd != 1, no bound on size that cannot be purchased */
    fprintf (fout, "0\n");
    return 0;
   }

  /* we can do 0 */
  cando[0] = 1;

  lv = pos = 0;
  last = 0;
  while (pos < 2000000000)
   { /* bound as stated */

    /* if last 256 were all possible, we are done */
    if (pos - last > 256) break; 

    if (!cando[lv]) 
      last = pos; /* this isn't possible, update last impossible */
    else 
     { /* this is possible */
      cando[lv] = 0; /* mark pos+256 as impossible */

      /* mark pos + size as possible for each package size */
      for (lv2 = 0; lv2 < nsize; lv2++)
        cando[(lv+sizes[lv2])%256] = 1;
     }

    /* update lv & pos */
    lv = (++pos) % 256; 
   }
  if (pos >= 2000000000) last = 0; /* shouldn't occur */

  fprintf (fout, "%i\n", last);
  return 0;
 }

Alex Schwendner's Comments

Given two relatively prime numbers N and M, the largest number that you cannot make is NM - M - N, that is, the product minus the sum. We do not have two numbers; however, even if we were using only two of them the answer could not exceed 256 * 256 - 256 - 256 = 65024 (much less then the 2,000,000,000 that we were guaranteed). It is therefore reasonable to have an array of 65024 booleans and work on all of them (If cando[x] then cando[x + sizes[p]]). If there is some number above 65024 that cannot be made then we know that there is no bound to the largest number. Because cando[0] is set to false, if every number can be made then the program will output '0' automatically. This program is shorter and easier to code, and although it is somewhat less efficient, it is easily able to solve the problem in the time limit.

#include <fstream.h>
#include <string.h>

int 
main ()
{

    int     n;
    int     sizes[10];

    ifstream filein ("nuggets.in");
    filein >> n;
    for (int in = 0; in < n; ++in) {
	filein >> sizes[in];
    }
    filein.close ();

    bool    cando[67000];
    memset (cando, 0, 67000);

    for (int loop = 0; loop < n; ++loop) {
	cando[sizes[loop]] = true;
	for (int loop2 = 0; loop2 < 66700; ++loop2) {
	    if (cando[loop2]) {
		cando[loop2 + sizes[loop]] = true;
	    }
	}
    }

    ofstream fileout ("nuggets.out");
    for (int out = 66500; out >= 0; --out) {
	if (!cando[out]) {
	    if (out < 66000) {
		fileout << out << endl;
		break;
	    }
	    else {
		fileout << 0 << endl;
		break;
	    }
	}
    }
    fileout.close ();

    return (0);
}

商品评论中的实体情感分析 在之前的文章中我已经将商品评论中的评价实体,也就是特征(feature)给挖掘出来了,我看了一下,挖掘的效果还是不错的。原文链接:http://blog.csdn.net/chixujohnny/article/details/52794685 下面一步要做的是施加情感因子,也就是说,要将人们评论时候所连带的感情程度施加一下。这项任务的归类应该是情感分析技术。 目前该类问题的情感分析技 阅读详情

相关推荐

CNN经典网络模型(二):AlexNet简介及代码实现(PyTorch超详细注释版)

CNN经典网络模型之一:AlexNet,本文包含其简介及代码,在PyTorch中实现,进行超详细注释,适合新生小白阅读学习~

qq_43307074的博客 2万+

C++——【USACO 4.1.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 possibl

McDonnell_Douglas的博客 583

Netty源码分析系列之writeAndFlush()下

write()只会将数据只是发送到了ChannelOutboundBuffer 缓冲区中,还没有被发送到操作系统的套接字中只有当调用了 flush()方法后,才会真正将数据发送到套接字中。那么 flush()方法的源码又是如何执行的呢?

天堂的博客 1575

Section 4.1 Beef McNuggets

/*ID: niepeng1PROG: nuggetsLANG: C++*//*这题解法玄妙,用了M,N两个素数,那么他们不能组成的最大数是M*N-M-N。然后对小于它的数目全部判断能否构成。如果有点大于最大的不能构成数并且没有被构成,那么就说明永远不可能有不能构成的最大值了。总是感觉正确性有点问题,但是呵呵,我又偷懒了。没有用滑动窗口的方法。闲着可以再思考思考。*/#include #defin

china1000的专栏 640

ADS Layout设计实战:从参数设置到协同仿真

本文详细介绍了ADS Layout设计的完整实战流程,从新建工程、单位设置、个性化偏好配置等基础操作入手,逐步深入到基板参数配置、端口设置与电磁仿真器选择等核心环节。重点阐述了参数化版图设计与扫描优化、以及原理图与Layout协同仿真的高级技巧,并通过一个微带线低通滤波器设计案例,完整演示了从理论到实现的闭环设计方法,帮助工程师高效、准确地完成高频电路版图设计与验证。

weixin_29268637的博客 818

如何快速学习 PHP 编程:从入门到实战的高效路径

PHP高效学习路线摘要:4-8周掌握Web开发核心技能。阶段1(1周)搭建XAMPP/MAMP环境,掌握基础语法和变量处理;阶段2(2周)重点学习表单处理、会话管理和PDO数据库操作(防SQL注入);阶段3(2-3周)实战开发个人博客等小项目。关键原则:边学边练(每个知识点配3-5个案例)、优先掌握Web刚需技能(HTTP请求、数据库、动态页面)、善用官方手册。避坑点:密码必须加密、SQL使用预处理、统一UTF-8编码。建议每天2-3小时实操,通过项目驱动学习,快速形成开发能力。

2501_94233638的博客 1402

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

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

weixin_34301132的博客 236

基于EKF的UWB-IMU融合定位系统在农业机器人中的抗干扰优化与Matlab仿真

本文探讨了基于扩展卡尔曼滤波(EKF)的UWB-IMU传感器融合定位系统在农业机器人中的应用。通过Matlab仿真验证,该系统有效解决了农业环境中UWB信号遮挡和IMU累积误差问题,实现了厘米级高精度定位,显著提升了机器人在复杂场景下的抗干扰能力和导航可靠性。

numpy6sculptor的博客 633

[USACO4.1.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 tr

qq_37665716的博客 322

【数据攻略】字节面试真题(含答案)+100道面试题库

数据攻略

qq_40002713的博客 4114

使用 MATLAB/Simulink 对一个开关磁阻电机进行建模

*开关磁阻电机(Switched Reluctance Motor, SRM)**是一种新型的无刷电机,以其简单的机械结构、宽广的调速范围和较高的效率在工业应用中得到了广泛关注。SRM的独特之处在于其定子和转子均为凸极结构,并且通过改变绕组电流的方向来实现转矩的产生和控制。本教程将详细介绍如何使用对一个开关磁阻电机进行建模,并探讨其基本控制策略,特别是针对双凸极结构的SRM。本文介绍了如何使用构建一个开关磁阻电机的双凸极结构模型,并探讨了其基本控制策略。

amy_mhd的博客 212

USACO Section 4.1 Beef McNuggets(数论 & 背包)

Beef McNuggetsHubert 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 possib...

angtongyou1893的博客 319

【人脸识别】基于 Gabor+SVM和PCA+SVM实现人脸识别matlab源码含 GUI

1 简介 随着经济的快速发展,互联网的普及,信息安全逐渐被人们所重视。人脸识别技术作为保护信息安全的重要手段之一,也逐渐被研究学者所重视。人脸识别作为计算机视觉技术和生物特征识别技术的一个重要分支,模式识别与人工智能的一个重要领域,其主要任务是对静态图像或动态视频进行识别。如何快速的正确的对人脸进行识别是目前人脸识别课题的一个难题。人脸识别算法的选取直接关系到人脸识别的识别率。 本文首先介绍了国内外人脸识别的发展现状,并对人脸图像预处理方法进行了详细阐述。图像预处理的方法有很多,主要包括:灰度变换、图像锐

qq_59747472的博客 1147

Usaco Training Section 4.1Beef McNuggets

Chapter4第一题就被卡了一下。 有n个数可以取,求每个数可以取无限次,问取的这些数的和不能表示的最大正整数是多少。 很显然的背包,但不知道边界,于是决定乱搞。 法一(乱搞):用map来做dp数组,正常转移,只要有连续256个数都能表示就结束。(因为每个数都&lt;=256)以为会超时,但神奇地过了。 乱搞代码: #include&lt;bits/stdc++.h&gt; #d...

Monster的博客 287

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

USACO-Section 4.1-PROB 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

So You Try 647

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

USACO Section 4.1 Beef McNuggets - 描述真吓人~

坑爹的描述说最大可能到2*10^9....我开始推的时候就推出最大只会到65535...还犹豫了好久~~结果用这个算法就过了~~最大的也就是65535...数据范围真心好吓人阿...      这道题很简单的说...首先看要输出0的情况...           1.没有得不到的数...那显然只有最小的盒子为1才行..特判就行了..           2.得不到的数是无穷的.恰恰是这个地方

zzyzzy12 1122

[USACO4.1]麦香牛块Beef McNuggets 题解报告

题目描述 农夫布朗的奶牛们正在进行斗争,因为它们听说麦当劳正在考虑引进一种新产品:麦香牛块。奶牛们正在想尽一切办法让这种可怕的设想泡汤。奶牛们进行斗争的策略之一是“劣质的包装”。“看,”奶牛们说,“如果你只用一次能装3块、6块或者10块的三种包装盒包装麦香牛块,你就不可能满足一次只想买1、2、4、5、7、8、11、14或者17块麦香牛块的顾客了。劣质的包装意味着劣质的产品。” 你的任...

weixin_30343157的博客 295

Beef McNuggets

题意:给定N种金额的货币,求最大的不能拼成的金额,如果这个金额不存在或者为无限大则输出0 解题思路: 设货币金额的集合为S,f[i] = 0代表金额为i不能组成,f[i] = 1代表金额能够组成DP问题,f[i + S[j]] = f[i],j在[1, N]中待编辑 代码:

原味 538

【USACO题库】4.1.1 Beef McNuggets麦香牛块

这道题目主要是如何判断当前不用循环下去,以后的都可以被组合到了,这就需要判断如果当前连续可以组合的已经大于min{a[i]}了,就可以break了,因为这样子就可以永远的组合下去了。 还有就是有一个特殊数据,特殊判断一下,如果循环次数大于1000000的话直接输出0 var flag:boolean; n,i,min,ans,t,max:longint;

Hello,I'm Proking. 1117

usaco Beef McNuggets

这两天贼烦,ccf炸了,还有一个烦心事。哎我都不知道自己能不能坚持下去了。马上期末考了。这段时间还是抓紧时间复习吧同时刷usaco的节奏要跟以前一样了,毕竟课少了。 题解: 只要你知道以下的数论结论,这道题就是水背包了: 有两个数p,q,且gcd(q,p)=1,则最大无法表示成px+qy(x>=0,y>=0)的数是pq-q-p(对于n>pq-q-p,都可以表示

silence401的博客 450

USACO Beef McNuggets 解题报告

这道题要总结的地方很多。 首先,做题要想下,否则很可能无限跑下去,没有结果。具体来说,所有number都可以买到对应的输入情况是有个option是1。这样无论多大的size,都可以用size个1买到。买不到的size没有上届对应着这些option的最大公约数大于1.这样能买到的size是有间隔的,这也意味着买不到的size是没有上届的。 其次,如果碰到“ Execution error: Y

thestoryofsnow的专栏 1086

分支限界法-0-1背包问题

分支限界法类似于回溯法,也是在问题的解空间上搜索问题解的算法。一般情况下,分支限界法与回溯法的求解目标不同。回溯法的求解目标是找出解空间中满足约束条件的所有解,而分支限界法的求解目标则是找出满足约束条件的一个解,或是在满足约束条件的解中找出使某一目标函数值达到极大或极小的解,即在某种意义下的最优解。 由于求解目标不同,导致分支限界法与回溯法对解空间的搜索也不相同。回溯法以深度优先的方式搜索解空间...

辣条不爱辣的博客 1万+

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

题目描述 农夫布朗的奶牛们正在进行斗争,因为它们听说麦当劳正在考虑引进一种新产品:麦香牛块。奶牛们正在想尽一切办法让这种可怕的设想泡汤。奶牛们进行斗争的策略之一是“劣质的包装”。“看,”奶牛们说,“如果你只用一次能装3块、6块或者10块的三种包装盒包装麦香牛块,你就不可能满足一次只想买1、2、4、5、7、8、11、14或者17块麦香牛块的顾客了。劣质的包装意味着劣质的产品。” 你的任务是帮助这

你的写轮眼,究竟还能看多远呢 1368

[4_1_nuggets] Dynamic Programming or Number Theory

http://www.nocow.cn/index.php/USACO/nuggets 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

MADNESS 702

USACO4.1.1 Beef McNuggets (nuggets)

所有方案都能满足的情况:当且仅当输入里的n个数里有1。否则1一定是一个解。 不存在不能满足的上限:计算出输入中最大的两个数的最小公倍数x,如果比x大的数有一个不能满足,则不存在不能满足的上限。 然后就是用背包问题统计某个数是否可以取到。/* ID:shijiey1 LANG:C++ PROG:nuggets */ #include #include #include using nam

Shion的专栏 663

USACO 4.1 Beef McNuggets

Beef McNuggetsHubert 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 possib...

weixin_30731287的博客 155
上一篇: hdu 4523 威威猫系列故事——过生日(简单大数相加比较)
下一篇: CF -62B - Tyndex.Brome(二分查找)
剑不飞
博客等级 码龄15年 86粉丝 505原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址