USACO Feed Ratios

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
ID: wcr19961
PROG: ratios
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = 5;
int a[maxn], b[maxn], c[maxn];

int main() {
freopen("ratios.in", "r", stdin);
freopen("ratios.out", "w", stdout);
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
for (int i = 0; i < 3; ++i) {
scanf("%d%d%d", &a[i], &b[i], &c[i]);
}
int ansi, ansj, ansk, ans = 0x3f3f3f3f;
for (int i = 0; i < 100; ++i) {
for (int j = 0; j < 100; ++j) {
for (int k = 0; k < 100; ++k) {
if (i + j + k == 0) {
continue;
}
int xx = i * a[0] + j * a[1] + k * a[2];
int yy = i * b[0] + j * b[1] + k * b[2];
int zz = i * c[0] + j * c[1] + k * c[2];
if ((x == 0 && xx != 0) || (y == 0 && yy != 0) || (z == 0 && zz != 0)) {
continue;
}
if ((x == 0 || xx % x == 0) && (y == 0 || yy % y == 0) && (z == 0 || zz % z == 0)) {
int tmp = x != 0 ? xx / x : (y != 0 ? yy / y : zz / z);
if (tmp < ans && (x == 0 || xx / x == tmp) && (y == 0 || yy / y == tmp) && (z == 0 || zz / z == tmp)) {
ans = tmp;
ansi = i;
ansj = j;
ansk = k;
}
}
}
}
}
if (ans == inf) {
puts("NONE");
} else {
printf("%d %d %d %d\n", ansi, ansj, ansk, ans);
}
return 0;
}