QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#739612 | #9622. 有限小数 | Wood# | WA | 113ms | 3720kb | C++23 | 2.5kb | 2024-11-12 22:24:46 | 2024-11-12 22:24:53 |
Judging History
answer
#include <bits/stdc++.h>
using i64 = long long;
constexpr int MAXD = 1E9;
i64 exgcd(i64 a, i64 b, i64 &x, i64 &y) {
if (b == 0) {
x = 1, y = 0;
return a;
}
i64 g = exgcd(b, a % b, y, x);
y -= a / b * x;
return g;
}
void solve() {
int a, b;
std::cin >> a >> b;
int t = b;
while (t % 2 == 0) {
t /= 2;
}
while (t % 5 == 0) {
t /= 5;
}
int ansc = MAXD, ansd = MAXD;
for (int p2 = 1; p2 * t <= MAXD; p2 *= 2) {
for (int p5 = 1; 1LL * p2 * p5 * t <= MAXD; p5 *= 5) {
i64 d = 1LL * p2 * p5 * t;
i64 k, c;
i64 pa = d * b, pb = b, pd = d * a;
i64 g = exgcd(pa, pb, k, c);
c *= -1;
if (pd % g) {
continue;
}
k *= pd / g, c *= pd / g;
pa /= g, pb /= g, pd /= g;
assert(k * pa - c * pb == pd);
while (c < 0) {
c += pa;
k += pb;
}
while (c >= pa) {
c -= pa;
k -= pb;
}
// assert(k * pa - c * pb == pd);
// i64 v = pd % pa * c % pa;
// if (v < 0) {
// v += pa;
// }
// if (d == 14) {
// std::cout << k << " " << pa << " " << c << " " << pb << " " << pd << " ??\n";
// }
int v2 = 0, v5 = 0;
i64 now = pa;
while (now % 2 == 0) {
now /= 2;
v2 += 1;
}
while (now % 5 == 0) {
now /= 5;
v5 += 1;
}
i64 v = 1E9;
for (int t2 = 0; t2 <= v2; t2++) {
i64 w = pa >> t2;
for (int t5 = 0; t5 <= v5; t5++) {
if (w >= pd && (w - pd) % pb == 0) {
v = std::min(v, (w - pd) / pb);
}
if (!w) {
break;
}
w /= 5;
}
}
if (v < ansc) {
ansc = v;
ansd = d;
}
}
}
std::cout << ansc << " " << ansd << "\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int T;
std::cin >> T;
while (T--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3660kb
input:
4 1 2 2 3 3 7 19 79
output:
0 2 1 3 1 14 3 316
result:
ok 4 case(s)
Test #2:
score: -100
Wrong Answer
time: 113ms
memory: 3720kb
input:
10000 11 12 28 53 17 60 2 35 17 181 80 123 68 141 79 163 71 99 13 64 33 61 15 32 16 61 11 86 33 74 128 143 40 53 7 23 30 31 5 6 86 181 73 91 13 23 71 81 1 2 7 38 117 160 33 83 129 151 88 153 25 58 16 19 19 141 95 124 43 96 71 139 11 59 106 109 93 152 34 43 17 99 1 57 20 159 16 25 5 73 159 170 172 17...
output:
1 12 25 53 13 60 3 70 11 1810 43 123 5 282 5 326 28 99 3 64 28 61 1 32 29 122 21 172 4 74 15 143 13 53 9 46 1 31 1 6 9 362 18 91 10 23 10 81 0 2 3 190 43 160 17 166 22 151 65 153 4 58 3 19 46 705 29 124 5 96 68 139 4 295 3 109 59 152 9 43 14 495 7 2850 59 795 9 25 23 730 11 170 7 179 3 65 32 170 7 1...
result:
wrong answer Jury found better answer than participant's 1 < 25 (Testcase 2)