提出 #22302758
ソースコード 拡げる
#include <stdio.h>
long long gcd(long long a, long long b, long long *x, long long *y) {
long long ans = 0;
long long tmp = 0;
if (a < b) {
return gcd(b ,a, y, x);
}
if (b < 2 || a % b == 0) {
*x = 0;
*y = 1;
return b;
}
ans = gcd(b, a % b, x, y);
tmp = *x;
*x = *y;
*y = tmp - (a / b) * *y;
return ans;
}
long long main () {
long long n = 0;
long long c[3] = {};
int res = 0;
long long trans[3][3] = {};
long long ans = 10000;
long long max_c_res[3] = {};
long long gcd_ab = 0;
long long gcd_abc = 0;
long long step = 0;
long long x = 0;
long long y = 0;
long long mult_ab = 0;
long long diff_steps = 0;
long long dummy_x = 0;
long long dummy_y = 0;
int isFinished = 0;
res = scanf("%lld", &n);
res = scanf("%lld", c);
res = scanf("%lld", c+1);
res = scanf("%lld", c+2);
if (c[1] < c[0]) {
long long tmp = c[1];
c[1] = c[0];
c[0] = tmp;
}
if (c[2] < c[1]) {
long long tmp = c[2];
c[2] = c[1];
c[1] = tmp;
}
if (c[1] < c[0]) {
long long tmp = c[1];
c[1] = c[0];
c[0] = tmp;
}
for (int i = 0; i < 3; i++) {
for (int j = i; j < 3; j++) {
long long gcd_ij = gcd(c[i], c[j], &dummy_x, &dummy_y);
trans[i][j] = c[j] / gcd_ij;
trans[j][i] = c[i] / gcd_ij;
}
}
gcd_ab = gcd(c[0], c[1], &x, &y);
gcd_abc = gcd(gcd_ab, c[2], &mult_ab, max_c_res+2);
if (n % gcd_abc != 0) {
return 0;
}
max_c_res[2] *= n / gcd_abc;
max_c_res[1] = mult_ab * y * (n / gcd_abc);
max_c_res[0] = mult_ab * x * (n / gcd_abc);
step = gcd_ab / gcd_abc;
x *= c[2] / gcd_abc;
y *= c[2] / gcd_abc;
diff_steps = ((n / c[2]) - max_c_res[2]) / step;
max_c_res[2] += diff_steps * step;
max_c_res[1] -= diff_steps * y;
max_c_res[0] -= diff_steps * x;
while(max_c_res[2] >= 0) {
if (max_c_res[0] < 0) {
long long k = 1 + (-max_c_res[0] / trans[0][1]);
max_c_res[0] += k * trans[0][1];
max_c_res[1] -= k * trans[1][0];
} else {
long long k = max_c_res[0] / trans[0][1];
max_c_res[0] -= k * trans[0][1];
max_c_res[1] += k * trans[1][0];
}
if (ans > max_c_res[0] + max_c_res[1] + max_c_res[2]) {
ans = max_c_res[0] + max_c_res[1] + max_c_res[2];
}
max_c_res[2] -= step;
max_c_res[1] += y;
max_c_res[0] += x;
}
printf("%lld\n", ans);
return 0;
}
提出情報
ジャッジ結果
| セット名 |
Sample |
All |
| 得点 / 配点 |
0 / 0 |
0 / 3 |
| 結果 |
|
|
| セット名 |
テストケース |
| Sample |
sample_1.txt, sample_2.txt, sample_3.txt, sample_4.txt |
| All |
rand_01.txt, rand_02.txt, rand_03.txt, rand_04.txt, rand_05.txt, rand_06.txt, rand_07.txt, rand_08.txt, rand_09.txt, rand_10.txt, rand_large_abc_1.txt, rand_large_abc_2.txt, rand_large_abc_3.txt, sample_1.txt, sample_2.txt, sample_3.txt, sample_4.txt, small_1.txt, small_2.txt, small_3.txt, small_4.txt |
| ケース名 |
結果 |
実行時間 |
メモリ |
| rand_01.txt |
WA |
8 ms |
1640 KiB |
| rand_02.txt |
AC |
2 ms |
1636 KiB |
| rand_03.txt |
AC |
1 ms |
1640 KiB |
| rand_04.txt |
WA |
1 ms |
1636 KiB |
| rand_05.txt |
AC |
1 ms |
1640 KiB |
| rand_06.txt |
AC |
1 ms |
1636 KiB |
| rand_07.txt |
WA |
1 ms |
1636 KiB |
| rand_08.txt |
WA |
1 ms |
1696 KiB |
| rand_09.txt |
WA |
1 ms |
1636 KiB |
| rand_10.txt |
AC |
1 ms |
1636 KiB |
| rand_large_abc_1.txt |
AC |
1 ms |
1704 KiB |
| rand_large_abc_2.txt |
AC |
1 ms |
1636 KiB |
| rand_large_abc_3.txt |
AC |
1 ms |
1632 KiB |
| sample_1.txt |
AC |
1 ms |
1640 KiB |
| sample_2.txt |
AC |
1 ms |
1636 KiB |
| sample_3.txt |
AC |
1 ms |
1632 KiB |
| sample_4.txt |
AC |
1 ms |
1688 KiB |
| small_1.txt |
AC |
1 ms |
1640 KiB |
| small_2.txt |
AC |
1 ms |
1632 KiB |
| small_3.txt |
AC |
1 ms |
1632 KiB |
| small_4.txt |
AC |
1 ms |
1640 KiB |