QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#814659 | #9622. 有限小数 | sgweo8ys | WA | 18ms | 7940kb | C++14 | 1.3kb | 2024-12-14 19:17:44 | 2024-12-14 19:17:45 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1000010;
const int mx = 1000000000;
int T, a, b, c, d, x, y, pw2[N], pw5[N], rs[N], cnt, ans, ansr;
int Exgcd(int a, int b, int &x, int &y) {
if (!b) {
x = 1;
y = 0;
return a;
}
int d = Exgcd(b, a % b, x, y);
int t = x;
x = y;
y = t - (a / b) * y;
return d;
}
void solve()
{
ans = mx + 1;
scanf("%lld%lld", &a, &b);
int k = 1;
while(b % 2 == 0) k *= 2, b /= 2;
while(b % 5 == 0) k *= 5, b /= 5;
if(b == 1) return puts("0 1"), void();
int t = 1;
for(int i = 1; i <= cnt; i++){
t = rs[i];
if(t * b > mx) continue;
long long val = 1ll * a * t % b;
val = b - val;
Exgcd(k, b, x, y);
val = 1ll * x * val % b;
if(val < ans) ans = val, ansr = b * t;
}
printf("%lld %lld\n", ans, ansr);
}
signed main()
{
pw2[0] = pw5[0] = 1;
for(int i = 1; i < 30; i++) pw2[i] = pw2[i - 1] * 2;
for(int i = 1; i < 13; i++) pw5[i] = pw5[i - 1] * 5;
for(int i = 0; i <= 29; i++){
for(int j = 0; j <= 12; j++){
long long r = 1ll * pw2[i] * pw5[j];
if(r <= mx) rs[++cnt] = r;
}
}
sort(rs + 1, rs + cnt + 1);
scanf("%lld", &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: 7940kb
input:
4 1 2 2 3 3 7 19 79
output:
0 1 1 3 1 14 3 316
result:
ok 4 case(s)
Test #2:
score: -100
Wrong Answer
time: 18ms
memory: 7916kb
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 3 1 54272 -2 6 1 7 1 231680000 23 3936 1 36096000 5 326 1 63360 0 1 1 31232 0 1 1 4880 -42 10750 -36 18500 1 11714560 1 331250 1 2944 1 31 -2 6 1 289600000 1 455000 1 58880 1 51840 0 1 -18 304 0 1 1 415 1 19328000 1 765000000 -28 4640 1 608 1 72192 3 775 -2 3 3 347500 1 944 1 43600 -18 76 1 430000...
result:
wrong answer Integer -2 violates the range [0, 10^9]