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.
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. |
3 4 5 1 2 3 3 7 1 2 1 2
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.
8 1 5 7
农夫约翰从来只用调配得最好的饲料来喂他的奶牛。饲料用三种原料调配成:大麦,燕麦和小麦。他知道自己的饲料精确的配比,在市场上是买不到这样的饲料的。他只好购买其他三种混合饲料(同样都由三种麦子组成),然后将它们混合,来调配他的完美饲料。
给出三组整数,表示 大麦:燕麦:小麦 的比例,找出用这三种饲料调配 x:y:z 的饲料的方法。
例如,给出目标饲料 3:4:5 和三种饲料的比例:
1:2:3 3:7:1 2:1:2
你必须编程找出使这三种饲料用量最少的方案,要是不能用这三种饲料调配目标饲料,输出“NONE”。“用量最少”意味着三种饲料的用量(整数)的和必须最小。 (应该是尽可能的让前一份小。 对于 5 8 0 3 5 4 1 3 0 6 2 0 我们要输出0_38_7_16而不是3_5_1_4) 对于上面的例子,你可以用8份饲料1,1份饲料2,和5份饲料3,来得到7份目标饲料:
8*(1:2:3) + 1*(3:7:1) + 5*(2:1:2) = (21:28:35) = 7*(3:4:5)
表示饲料比例的整数,都是小于100的非负整数。表示各种饲料的份数的整数,都小于100。一种混合物的比例不会由其他混合物的比例直接相加得到。
PROGRAM NAME: ratios
INPUT FORMAT:
(file ratios.in)
Line 1: 三个用空格分开的整数,表示目标饲料
Line 2..4: 每行包括三个用空格分开的整数,表示农夫约翰买进的饲料的比例
OUTPUT FORMAT:
(file ratios.out)
输出文件要包括一行,这一行要么有四个整数,要么是“NONE”。前三个整数表示三种饲料的份数,用这样的配比可以得到目标饲料。第四个整数表示混合三种饲料后得到的目标饲料的份数。
3 4 5 1 2 3 3 7 1 2 1 2
8 1 5 7
枚举。
/* ID:sephiro5 LANG:C++ PROG:ratios */ #include#include #define MAXINT 100000000 using namespace std; int org[3]; int a[3][3]; int ans[3],tem[3]; int times[3]; int anss,fin; int main() { freopen("ratios.in","r",stdin); freopen("ratios.out","w",stdout); for (int i=0;i<3;++i) { scanf("%d",&org[i]); if (!org[i]) org[i]=-1; } for (int i=0;i<3;++i) for (int j=0;j<3;++j) scanf("%d",&a[i][j]); anss=MAXINT; for (times[0]=0;times[0]<=100;++times[0]) for (times[1]=0;times[1]<=100;++times[1]) for (times[2]=0;times[2]<=100;++times[2]) { if ((!times[0])&&(!times[1])&&(!times[2])) continue; fill_n(tem,3,0); for (int s=0;s<3;++s) for (int t=0;t<3;++t) tem[t]+=a[s][t]*times[s]; bool ok=1; for (int s=0;s<3;++s) if (org[s]!=-1) if ((tem[s]%org[s])!=0) { ok=0; break; } if (ok) { int tt=tem[0]/org[0]; for (int s=1;s<3;++s) if (org[s]!=-1) if ((tem[s]/org[s])!=tt) { ok=0; break; } } if (ok) { int tems=0; for (int s=0;s<3;++s) tems+=times[s]; if (tems