USACO3.2.4--Feed 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 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

 题解:看到题目中的系数都会小于100,即枚举量等于10^3。果断枚举了。要注意被零除的情况。效率还不错,最慢的0.011s。据说还有解方程的。。全部0s秒杀,不过我不会。。。

View Code
 1 /*
 2 ID:spcjv51
 3 PROG:ratios
 4 LANG:C
 5 */
 6 #include<stdio.h>
 7 #include<string.h>
 8 struct tatio
 9 {
10     int x,y,z;
11 };
12 int main(void)
13 {
14     freopen("ratios.in","r",stdin);
15     freopen("ratios.out","w",stdout);
16     int x,y,z,i,j,k,t,a,b,c;
17     long a1,b1,c1;
18     struct tatio rt[3];
19     scanf("%d%d%d",&x,&y,&z);
20     for(i=0; i<3; i++)
21         scanf("%d%d%d",&rt[i].x,&rt[i].y,&rt[i].z);
22     for(i=0; i<100; i++)
23         for(j=0; j<100; j++)
24             for(k=0; k<100; k++)
25                 if(i!=0||j!=0||k!=0)
26                 {
27 
28                     a1=i*rt[0].x+j*rt[1].x+k*rt[2].x;
29                     b1=i*rt[0].y+j*rt[1].y+k*rt[2].y;
30                     c1=i*rt[0].z+j*rt[1].z+k*rt[2].z;
31                     if(a1!=0&&x==0) continue;
32                     if(b1!=0&&y==0) continue;
33                     if(c1!=0&&z==0) continue;
34                     t=a1/x;
35                     if(a1%x==0)
36                     {
37                         a=t*x;
38                         b=t*y;
39                         c=t*z;
40                         if(a==a1&&b==b1&&c==c1)
41                         {
42                             printf("%d %d %d %d\n",i,j,k,t);
43                             return 0;
44                         }
45                     }
46 
47                 }
48     printf("NONE\n");
49     return 0;
50 }

 

posted on 2013-02-26 11:14  仗剑奔走天涯  阅读(218)  评论(0编辑  收藏  举报

导航