USACO section 3.2 Feed Ratios(高斯定理)

高斯定理的证明(三重积分的C/C++实现)(C++)(大学物理) 特别鸣谢:无私帮助我的ycc同学 #include <iostream> #include <vector> #include <random> #include <chrono> #include <corecrt_math_defines.h> struct Eletrons {//将电子的各个参量封装到一个结构体,便于访问 double x, y, z, q; }; struct Vec {//“面向对象” double x, y, 阅读详情
Feed Ratios
1998 ACM Finals, Dan Adkins

Farmer John feeds his cows only the finest mixture of cow food, which has three components: Barley, Oats, and Wheat. While he knows the precise mixture of these easily mixable grains, he can not buy that mixture! He buys three other mixtures of the three grains and then combines them to form the perfect mixture.

Given a set of integer ratios barley:oats:wheat, find a way to combine them IN INTEGER MULTIPLES to form a mix with some goal ratio x:y:z.

For example, given the goal 3:4:5 and the ratios of three mixtures:

        1:2:3
        3:7:1
        2:1:2

your program should find some minimum number of integer units (the `mixture') of the first, second, and third mixture that should be mixed together to achieve the goal ratio or print `NONE'. `Minimum number' means the sum of the three non-negative mixture integers is minimized.

For this example, you can combine eight units of mixture 1, one unit of mixture 2, and five units of mixture 3 to get seven units of the goal ratio:

    8*(1:2:3) + 1*(3:7:1) + 5*(2:1:2) = (21:28:35) = 7*(3:4:5)

Integers in the goal ratio and mixture ratios are all non-negative and smaller than 100 in magnitude. The number of units of each type of feed in the mixture must be less than 100. The mixture ratios are not linear combinations of each other.

PROGRAM NAME: ratios

INPUT FORMAT

 

Line 1:Three space separated integers that represent the goal ratios
Line 2..4:Each contain three space separated integers that represent the ratios of the three mixtures purchased.

SAMPLE INPUT (file ratios.in)

3 4 5
1 2 3
3 7 1
2 1 2

OUTPUT FORMAT

The output file should contain one line containing four integers or the word `NONE'. The first three integers should represent the number of units of each mixture to use to obtain the goal ratio. The fourth number should be the multiple of the goal ratio obtained by mixing the initial feed using the first three integers as mixing ratios.

SAMPLE OUTPUT (file ratios.out)

8 1 5 7


思路:用高斯消元算出配置1份目标原料所需的具体配比(这是个小数,我们用分数来表示),然后分母求最小公倍数LCM就是目标配比份额,在将其余的依次乘上LCM就是

          所需原料配比。

失误点:注意出现不够分的情况输出NONE,即是矩阵的行中出现有正有负则不可能完成配料。

 

具体代码:

/*
ID:nealgav1
LANG:C++
PROG:ratios
*/
#include<fstream>
using namespace std;
ifstream cin("ratios.in");
ofstream cout("ratios.out");
const int mm=110;
int map[mm][mm+1];
class node{public:int a,b;node(){a=0,b=1;}}ans[mm];
int var,equ;
void debug();
int gcd(int a,int b)
{ int z;
  while(b!=0)
  {
    z=b;b=a%b;a=z;
  }
  return a;
}
int lcm(int a,int b)
{
  return a*b/gcd(a,b);
}
void swap(int &a,int&b)
{
  int z;z=a;a=b;b=z;
}
int abss(int a)
{
  if(a<0)return -a;
  return a;
}
void Guss()
{ int k,col;
  for(k=0,col=0;k<var&&col<equ;k++,col++)
  {
    int i_max=k;
    for(int i=k+1;i<equ;i++)
    if(map[i][col]>i_max)i_max=i;
    if(i_max!=k)
    {
      for(int i=col;i<var+1;i++)
      swap(map[i_max][i],map[k][i]);
    }
    if(map[k][col]==0){k--;continue;}
    for(int i=k+1;i<equ;i++)
    if(map[i][col]!=0)
    {
      int LCM=lcm(abss(map[i][col]),abss(map[k][col]));
      int ta=LCM/map[i][col],tb=LCM/map[k][col];
      if(ta*tb<0)ta=-ta;
      for(int j=col;j<var+1;j++)
      map[i][j]=map[i][j]*ta-map[k][j]*tb;
      ///如果全为负数则改为正数
      if(map[i][var]<0)
      for(int j=col;j<var+1;j++)map[i][j]=-map[i][j];
    }
  }
  for(int i=k;i<equ;i++)
  if(map[i][col]==0)
  {
    cout<<"NONE\n";
    return;
  }
  ///矩阵出现是负数的说明有的不够数,所以无答案
  for(int i=0;i<equ;i++)
  {for(int j=i;j<var+1;j++)
   {
     if(map[i][j]<0){cout<<"NONE\n";return;}
   }
  }
  int LCM=1,GCD=1;
  for(int i=k-1;i>=0;i--)
  {
    node tmp;tmp.a=map[i][var];tmp.b=1;
    for(int j=i+1;j<var;j++)
    if(map[i][j])
    {
      LCM=lcm(tmp.b,ans[j].b);
      tmp.a=(LCM/tmp.b)*tmp.a-(LCM/ans[j].b)*ans[j].a*map[i][j];
      GCD=gcd(LCM,tmp.a);
      tmp.a=tmp.a/GCD;tmp.b=LCM/GCD;
    }
    ans[i].b=tmp.b*map[i][i];
    GCD=gcd(ans[i].b,tmp.a);
    ans[i].b=ans[i].b/GCD;
    ans[i].a=tmp.a/GCD;
  }
  LCM=1;
  for(int i=0;i<var;i++)
  LCM=lcm(ans[i].b,LCM);
  for(int i=0;i<var;i++)
  cout<<(LCM/ans[i].b)*ans[i].a<<" ";
  cout<<LCM<<"\n";
}
void debug()
{
  for(int i=0;i<equ;i++)
  {for(int j=0;j<var+1;j++)
   cout<<map[i][j]<<" ";
   cout<<"\n";
  }
}
int main()
{ var=equ=3;
  for(int i=0;i<3;i++)
  cin>>map[i][3];
  for(int i=0;i<3;i++)
  for(int j=0;j<3;j++)
  cin>>map[j][i];
  ///debug();
  Guss();
  cin.close();cout.close();
}


 

 

 

 

 

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

Compiling...
Compile: OK

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

All tests OK.

YOUR PROGRAM ('ratios') WORKED FIRST TIME! That's fantastic -- and a rare thing. Please accept these special automated congratulations.

Here are the test data inputs:

------- test 1 ----
5 8 0
3 5 4
1 3 0
6 2 0
------- test 2 ----
3 4 5
1 2 3
3 7 1
2 1 2
------- test 3 ----
20 22 24
4 5 5
9 5 6
12 12 14
------- test 4 ----
80 85 80
98 97 96
89 87 88
78 79 80
------- test 5 ----
99 99 99
1 0 0
0 1 0
0 0 1
------- test 6 ----
93 95 97
98 0 0
0 98 0
0 0 98

Keep up the good work!

Feed Ratios
Hal Burch

As there are only 1013 = 1,030,301 ways to do this, try them all and pick the best.

#include <stdio.h>

/* the goal ratio */
int goal[3];

/* the ratio of the feeds */
int ratios[3][3];

/* the best solution found so far */
int min;
int mloc[4]; /* amounts of ratio 1, 2, and 3, and the amount of ratio 4 prod */

/* the amount of each type of component in the feed */
int sum[3];

int main(int argc, char **argv)
 {
  FILE *fout, *fin;
  int lv, lv2, lv3; /* loop variables */
  int t, s; /* temporary variables */
  int gsum; /* the sum of the amounts of components in the goal mixture */

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

  /* read in data */
  fscanf (fin, "%d %d %d", &goal[0], &goal[1], &goal[2]);
  for (lv = 0; lv < 3; lv++)
    fscanf (fin, "%d %d %d", ratios[lv]+0, ratios[lv]+1, ratios[lv]+2);

  gsum = goal[0] + goal[1] + goal[2];

  min = 301; /* worst than possible = infinity */

  /* boundary case (this ensures gsum != 0) */
  if (gsum == 0)
   {
    fprintf (fout, "0 0 0 0\n");
    return 0;
   }

  for (lv = 0; lv < 100; lv++)
    for (lv2 = 0; lv2 < 100; lv2++)
     { /* for each amout of the first two types of mixtures */
      sum[0] = lv*ratios[0][0] + lv2*ratios[1][0];
      sum[1] = lv*ratios[0][1] + lv2*ratios[1][1];
      sum[2] = lv*ratios[0][2] + lv2*ratios[1][2];

      if (lv + lv2 > min) break;

      for (lv3 = 0; lv3 < 100; lv3++)
       {
        s = lv + lv2 + lv3;
	if (s >= min) break; /* worse than we've found already */

        /* calculate a guess at the multiples of the goal we've obtained */
	/* use gsum so we don't have to worry about divide by zero */
        t = (sum[0] + sum[1] + sum[2]) / gsum;
	if (t != 0 && sum[0] == t*goal[0] && 
	        sum[1] == t*goal[1] && sum[2] == t*goal[2])
	 { /* we've found a better solution! */
	  /* update min */
	  min = s;

	  /* store the solution */
	  mloc[0] = lv;
	  mloc[1] = lv2;
	  mloc[2] = lv3;
	  mloc[3] = t;
	 }

        /* add another 'bucket' of feed #2 */
        sum[0] += ratios[2][0];
        sum[1] += ratios[2][1];
        sum[2] += ratios[2][2];
       }
     }
  if (min == 301) fprintf (fout, "NONE\n"); /* no solution found */
  else fprintf (fout, "%d %d %d %d\n", mloc[0], mloc[1], mloc[2], mloc[3]);
  return 0;
 }

Vlad Novakovski's Solution

When you combine multiples of mixtures, you can look at it as a multiplication of a matrix by a vector. For example, 8*(1:2:3) + 1*(3:7:1) + 5*(2:1:2) = (21:28:35) = 7*(3:4:5) can be seen as

[ 1 3 2 ]   [ 8 ]            
[ 2 7 1 ] * [ 1 ] = 7 [3 4 5]
[ 3 1 2 ]   [ 5 ]           

The matrix and the goal ratio vector (3:4:5 in this case) are given; what we have to find is the multiple vector (8:1:5 in this case) and the proportionality costant (7 here). This is like solving a system of linear equations. We can write it as

AX = kB.

Now, if we use Cramer's Rule, and let D = determinant of A, then

X_1 = k D_1 / D
X_2 = k D_2 / D
X_3 = k D_3 / D,

where D_1 is the determinant of the matrix A with the 1st column is replaced by B, D_2 is the determinant of the matrix A with the 2nd column is replaced by B, D_3 is the determinant of the matrix A with the 3rd column is replaced by B. (see a Linear Algebra textbook on why this works.) ,P> We are looking for integral solutions. If D = 0, no solutions. Otherwise, let k = D, and then X_1 = D_1, etc. If these values (X_1,X_2,X_3, _and_ k) all have a greatest common factor above 1, divide them all by that factor, as we are looking for the smallest possible solutions.

Now, if some of the numbers is greater than 100, we have not found a feasible solution, so we output `NONE'. Otherwise, the triple (X_1,X_2,X_3) is output, as well as the value k.

 

USACO section 3.4 Closed Fences(计算几何+叉积+二分) Closed Fences A closed fence in the plane is a set of non-crossing, connected line segments with N corners (3 i, yi}, i in (1..N). Every pair of adjacent vertices defines a side of the fence. 阅读详情

相关推荐

高斯函数和C++简单实现

高斯函数简单C++实现;

bcbobo21cn的专栏 2634

大学物理-三-----电磁学(高斯定理,电场部分)

高斯定理,场强,电势,电场中的欧姆定律,电阻率,电动势和终端电压

y江江江江的博客 1万+

告别MATLAB依赖:手把手教你用Python实现GCC-PHAT时延估计(附完整代码与对比测试)

本文详细介绍了如何使用Python实现GCC-PHAT时延估计算法,替代传统的MATLAB依赖。通过完整的代码示例和性能对比测试,展示了Python在信号处理领域的强大能力,特别是在广义互相关和TDOA应用中的高效实现。文章还提供了实时音频处理和嵌入式部署的实用技巧,帮助工程师快速掌握这一关键技术。

weixin_42667269的博客 221

USACO Section 3.2 Feed Ratios (ratios)

Feed Ratios 1998 ACM Finals, Dan Adkins Farmer John feeds his cows only the finest mixture of cow food, which has three components: Barley, Oats, and Wheat. While he knows the precise mixtur...

weixin_30681121的博客 179

【DAY11】超参数调整专题1

核心优势: 它不是随机选择下一个点,而是根据先前评估的结果建立一个概率模型(通常是高斯过程),预测哪些参数组合可能产生更好的结果,并据此选择下一个评估点。这使得它在寻找最优解方面通常比随机搜索更高效(用更少的迭代次数达到相似或更好的性能),特别是当模型训练(单次评估)非常耗时的时候。实际上,有着非常多的方式可以实现贝叶斯优化,上面的代码比较简洁美观,贝叶斯优化和网格搜索的代码书写风格高度一致。- 需要定义参数的搜索空间,与随机搜索类似,当搜索空间非常大时,它通常比网格搜索和随机搜索更有效。

daomingwu1的博客 747

PCB激光直接成像(LDI)设备技术解析与优化方案

激光直接成像(LDI)是PCB制造中的关键数字化工艺,通过激光束直接在基板上形成电路图案。其核心技术在于高精度光学系统与智能控制算法的结合,工作精度可达微米级。在HDI板、柔性电路等高端PCB制造中,LDI技术能显著提升线宽精度和层间对位准确性。当前行业面临材料适配性、热变形控制等挑战,动态能量补偿、多波长曝光等创新方案正在突破这些瓶颈。随着模块化设计、预测性维护等工程实践的普及,LDI设备综合效率持续提升,为5G、汽车电子等领域的精密电路生产提供关键技术支撑。

weixin_29168393的博客 393

USACO Training Section 3.2 Feed Ratios

[url="http://ace.delos.com/usacoprob2?a=JbTv7diNbha&S=ratios"]英文原题[/url] [url="http://www.wzoi.org/usaco/14%5C403.asp"]中文题译[/url] 大意:给出整数a[i][j]和b[i],i,j=1,2,3,有X={x[i]|i=1,2,3,s.t 存在y,对j=1,2...

山中竹笋 189

MRST开源油藏模拟:从入门到精通的全流程指南

本文全面介绍了MRST开源油藏模拟工具的使用方法,从环境搭建到核心功能实战,再到性能优化和工业级应用。MRST作为一款基于MATLAB的开源工具,为油藏工程师提供了强大的数值模拟能力,特别适合教学和科研场景。文章还分享了二次开发技巧和常见问题解决方案,帮助读者快速掌握油藏模拟技术。

beer8的博客 326

USACO Section 3.2 Feed Ratios(解三元一次方程)

Feed Ratios1998 ACM Finals, Dan Adkins Farmer John feeds his cows only the finest mixture of cow food, which has three components: Barley, Oats, and Wheat. While he knows the precise mixture of ...

angtongyou1893的博客 201

GPS卫星轨道的理论和计算笔记分享

广义相对论预测,对比在时空弯曲度较大的地面上的原子钟,运行于时空弯曲度较小的卫星原子钟在地面上看起来更快,每天快约45us;反过来,当电压施加在晶体上时,晶体形状会扭曲(比如说伸长),而当晶体被施加上反向电压后,它将做相反的变形运动(比如缩短)。X轴:指向参考子午面与地球赤道的一个交点;从北黄极上方观察,地球北极在空间的运动轨迹是一个近似于以北黄极为中心的顺时针方向旋转的圆周,圆周半径等于。从上万年周期看,地球北极绕北黄极做大圆周运动,而从几年的短期来看,北极又在某一点处做局部的小幅椭圆运动。

m0_57139844的博客 2004

基于Cadence IC617的共源级放大器设计:从直流到交流的virtuoso仿真实践

本文详细介绍了使用Cadence IC617的Virtuoso平台进行共源级放大器设计的完整仿真流程。从电路图绘制、直流工作点分析,到负载电阻RD的参数扫描及其对增益和带宽的影响,再到交流小信号频率响应仿真,提供了从理论到实践的全面指导,帮助初学者掌握模拟集成电路设计的核心仿真技能。

jj890的博客 480

USACO section 3.1 Contact(AC自动机)

Contact IOI'98 The cows have developed a new interest in scanning the universe outside their farm with radiotelescopes. Recently, they noticed a very curious microwave pulsing emission sent right f

nealgavin的专栏 9384

Python爬虫实战:用Playwright+Transformer做财经新闻事件驱动分析,我靠它抓住了算力政策的涨停机会

对我来说,这套系统最大的价值不是抓住了几次涨停,而是把“看新闻→分析→决策”的全流程自动化、量化了——不用再被股吧的情绪带偏,不用再熬夜刷新闻,而是用数据和模型做理性判断。更重要的是,搭建这套系统的过程,我吃透了Playwright动态爬虫、Transformer文本分析、量化数据对接这些核心技能,这些技能不管是做量化、做数据分析师,还是做其他领域的爬虫,都能用得上。现在打开PyCharm,跟着代码跑一遍,你也能从海量财经新闻里,挖出属于自己的事件驱动信号——与其靠感觉炒股,不如靠数据说话。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 1392

USCAO section 1.3 Calf Flac

Calf Flac It is said that if you give an infinite number of cows an infinite number of heavy-duty laptops (with very large keys), that they will ultimately produce all the world's great palindromes

nealgavin的专栏 6656

Unreal5从入门到精通 专栏目录

欢迎来到“UE5从入门到精通”专栏!在这里,我们将带您踏上一段精彩的旅程,深入探索虚幻引擎5(Unreal Engine 5)的无限可能。不论您是游戏开发的新手,还是希望提升技能的开发者,这里都有适合您的内容。基础知识:了解虚幻引擎5的界面、功能和基本操作,为您的学习打下坚实的基础。项目实战:通过实际项目案例,帮助您从理论走向实践,真实体验游戏开发的每个环节。高级技巧:分享各种高级功能和技巧,包括蓝图、C++编程、材质编辑、光照和渲染等,让您能够创作出更具专业水准的作品。

TxNet 2988

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

USACO section 1.5 Checker Challenge(dfs深搜)

Checker ChallengeExamine the 6x6 checkerboard below and note that the six checkers are arranged on the board so that one and only one is placed in each row and each column, and there is never more tha

nealgavin的专栏 1923

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

USACO section 2.3 Longest Prefix(dp)

Longest Prefix IOI'96 The structure of some biological objects is represented by the sequence of their constituents, where each part is denote by an uppercase letter. Biologists are interested in

nealgavin的专栏 1486

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

USACO section 2.4 Cow Tours(并查集+最短路)

Cow Tours Farmer John has a number of pastures on his farm. Cow paths connect some pastures with certain other pastures, forming a field. But, at the present time, you can find at least two pasture

nealgavin的专栏 1405

USACO section3.1 Shaping Regions(漂浮法)

Shaping Regions N opaque rectangles (1 <= N <= 1000) of various colors are placed on a white sheet of paper whose size is A wide by B long. The rectangles are put with their sides parallel to the s

nealgavin的专栏 1052

USACO section 2.2 Runaround Numbers

Runaround Numbers Runaround numbers are integers with unique digits, none of which is zero (e.g., 81362) that also have an interesting property, exemplified by this demonstration: If you start

nealgavin的专栏 989

USACO section 3.1 Stamps(dp)

Stamps Given a set of N stamp values (e.g., {1 cent, 3 cents}) and an upper limit K to the number of stamps that can fit on an envelope, calculate the largest unbroken list of postages from 1 cent

nealgavin的专栏 980

USCAO section 4.1 Fence Loops(最短路,最小环,5级)

Fence Loops The fences that surround Farmer Brown's collection of pastures have gotten out of control. They are made up of straight segments from 1 through 200 feet long that join together only a

nealgavin的专栏 941

USACO section 1.5 Superprime Rib(深搜)

Superprime RibButchering Farmer John's cows always yields the best prime rib. You can tell prime ribs by looking at the digits lovingly stamped across them, one by one, by FJ and the USDA. Farmer John

nealgavin的专栏 937

USACO section 2.2 Party Lamps

Party Lamps IOI 98 To brighten up the gala dinner of the IOI'98 we have a set of N (10 <= N <= 100) colored lamps numbered from 1 to N. The lamps are connected to four buttons: Button 1: Wh

nealgavin的专栏 913

USACO section 3.2 Spinning Wheels(模拟)

Spinning Wheels 1998 ACM NE Regionals Each of five opaque spinning wheels has one or more wedges cut out of its edges. These wedges must be aligned quickly and correctly. Each wheel also has an ali

nealgavin的专栏 875
上一篇: USACO section 3.2 Spinning Wheels(模拟)
下一篇: USACO section 3.2 Magic Squares(STL+广搜)
剑不飞
博客等级 码龄15年 86粉丝 505原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值