[Description]
Farmer John has always fed his cows with the best feed. Three kinds of raw materials are used for feed: barley, oatmeal and wheat. He knows the exact proportion of his feed and cannot buy it in the market. He had to buy three other types of Mixed Feed (also composed of three wheat) and then mix them to prepare his perfect feed.
Three integers are given to indicate the ratio of barley: oatmeal: wheat, and the method for allocating X: Y: Z with the three types of feed is found.
For example, give the proportion of the target feed at and the three types of feed:
1:2:3 3:7:1 2:1:2
You must program to find the solution that minimizes the three types of feed. If you cannot use these three types of feed to allocate the target feed, output "NONE ". "Minimum" means the sum of the three types of feed (integer) must be the minimum.
For the above example, you can use 8 portions of feed, 1 portions of feed, 2 portions, and 5 portions of feed 3 to get 7 portions of target feed:
8*(1:2:3) + 1*(3:7:1) + 5*(2:1:2) = (21:28:35) = 7*(3:4:5)
Indicates that the integer of the feed proportion and the target feed are all non-negative integers smaller than 100. Indicates the integer of the number of parts of each feed, all smaller than 100. The proportion of a mixture is not directly obtained from the proportion of the other mixture.
Format]
Program name: Ratios
Input Format:
(File ratios. In)
Line 1: Three integers separated by spaces, indicating the target feed.
Line 2 .. 4: each line contains three integers separated by spaces, indicating the proportion of the feed purchased by farmer John.
Output Format:
(File ratios. out)
The output file must contain one row. This row must have four integers or "NONE ". The first three integers indicate the number of parts of the three types of feed. You can use this ratio to obtain the target feed. The fourth integer indicates the number of parts of the target feed obtained after mixing the three types of feed.
[Analysis]
You can use enumeration to avoid timeout, but it will be slow.
Here we use the Gaussian elimination method to enumerate the target quantity. Then you can determine whether there is a solution.
1 # include <cstdlib> 2 # include <iostream> 3 # include <cstdio> 4 # include <cstring> 5 # include <cmath> 6 # include <iostream> 7 # include <fstream> 8 using namespace STD; 9 int data [100] [100]; 10 int A [100] [100]; // solves 11 void Init (INT num); 12 INT Gauss (int n ); // 13 void debug (); 14 int gcd (int A, int B) {return B = 0? A: gcd (B, A % B);} 15 int lcm (int A, int B) {return a * B/gcd (a, B );} 16 int main () 17 {18 int I; 19 // file operation 20 freopen ("ratios. in "," r ", stdin); 21 freopen (" ratios. out "," W ", stdout); 22 memset (data, 0, sizeof (data); 23 // input data 24 scanf (" % d ", & Data [3] [0], & Data [3] [1], & Data [3] [2]); 25 for (I = 0; I <3; I ++) scanf ("% d", & Data [I] [0], & Data [I] [1], & Data [I] [2]); 26 for (I = 1; I <= 100; I ++) 27 {28 Init (I ); // initialize 29 If (Gauss (I) Return 0; 30} 31 printf ("NONE"); 32 return 0; 33} 34 void Init (INT num) 35 {36 int I, j; 37 for (I = 0; I <3; I ++) // row and column 38 for (j = 0; j <3; j ++) A [I] [J] = data [J] [I]; 39 A [0] [3] = data [3] [0] * num; 40 A [1] [3] = data [3] [1] * num; 41 A [2] [3] = data [3] [2] * num; 42 43} 44 void debug () // debug 45 {46 int I, j; 47 for (I = 0; I <3; I ++) 48 {49 for (j = 0; j <4; j ++) printf ("% d", a [I] [J]); 50 printf ("\ n"); 51} 52 printf ("\ n"); 53} 54 int Gauss (INT m) 55 {56 int I = 0, j = 0, K, L, R, TMP, tmp1, tmp2, ANS [5]; 57 memset (ANS, 0, sizeof (ANS )); 58 While (I <3) & (j <4) // I is the number of rows. j currently needs to eliminate the number of rows 59 {60 r = I; // absolute value 61 for (k = I + 1; k <3; k ++) if (a [k] [J]> A [I] [J]) R = K; 62 if (R! = I) for (k = 0; k <4; k ++) Swap (A [I] [K], a [r] [k]); 63 64 if (a [I] [J]! = 0) 65 {66 for (L = I + 1; L <3; l ++) 67 if (a [l] [J]! = 0) 68 {69 TMP = lcm (ABS (A [l] [J]), ABS (A [I] [J]); 70 tmp1 = tmp/A [I] [J]; 71 tmp2 = tmp/A [l] [J]; 72 for (k = J; k <4; k ++) A [l] [k] = A [l] [k] * tmp2-A [I] [k] * tmp1; 73} 74 I ++; // number of rows 75} 76 J ++; 77} 78 // debug (); 79 if (I = 3) 80 {81 for (I = 2; i> = 0; I --) 82 {83 TMP = 0; 84 For (j = I + 1; j <3; j ++) TMP + = A [I] [J] * ans [J]; 85 86 If (A [I] [3]-TMP) % A [I] [I]! = 0) return false; 87 ans [I] = (a [I] [3]-TMP)/A [I] [I]; 88} 89 90 if (ANS [0] <0) | (ANS [1] <0) | (ANS [2] <0) return 0; // As long as 91 printf ("% d \ n", ANS [0], ANS [1], ANS [2], M ); 92 return 1; 93} 94 else return 0; 95}