QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#745957 | #9622. 有限小数 | Yurily# | WA | 0ms | 3888kb | C++20 | 2.2kb | 2024-11-14 12:39:24 | 2024-11-14 12:39:24 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
const LL e9 = 1e9;
const LL e18 = 1e18;
LL a, b, k;
LL b2, b5;
LL gcd(LL a, LL b){
LL r = a % b;
while(r > 0){
a = b;
b = r;
r = a % b;
}
return b;
}
LL div_ceil(LL x,LL y){
if(x>=0){
if(x%y)
return x/y+1;
return x/y;
}
else{
return -((-x)/y);
}
}
void extend_gcd(LL a,LL b,LL &x,LL &y){
if(b==0){
x=1;
y=0;
return;
}
extend_gcd(b,a%b,x,y);
LL tmp=x;
x=y;
y=tmp-(a/b)*y;
}
LL getf(LL a, LL b, LL c){
LL g=gcd(a,b);
LL aa=a/g,bb=b/g,cc=c/g;
LL xx,yy;
extend_gcd(aa,bb,xx,yy);
LL x0=xx*cc;
LL t=div_ceil(-x0,bb);
return x0+bb*t;
}
void sol(){
scanf("%lld%lld", &a, &b);
k = b;
b2 = b5 = 0;
while(k > 0 && k % 2 == 0){
b2++;
k /= 2;
}
while(k > 0 && k % 5 == 0){
b5++;
k /= 5;
}
if(k == 1){
printf("0 1\n");
return ;
}
LL minc = e18, mind = e18;
LL x, y, t, c, d, g;
for(x = 1; x <= e18 && b * x * y <= e18; x *= 2)
for(y = 1; y <= e18 && b * x * y <= e18; y *= 5){
t = k - (a * x * y % k);
c = t;
d = b * x * y;
g = gcd(c, d);
c /= g;
d /= g;
if(c < minc && d <= e9){
minc = c;
mind = d;
}
}
for(LL xi = 0, x = 1; xi <= b2; xi++, x *= 2){
for(LL yi = 0, y = 1; yi <= b5; yi++, y *= 5){
LL p = x * y;
LL q = k - a % k;
//p * x1 + k * y1 === q
//t = min(x1)
t = getf(p, k, q);
c = t;
d = b / p;
g = gcd(c, d);
c /= g;
d /= g;
if(c < minc){
minc = c;
mind = d;
}
}
}
printf("%lld %lld\n", minc, mind);
}
int main(){
int T;
scanf("%d", &T);
while(T--)
sol();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3888kb
input:
4 1 2 2 3 3 7 19 79
output:
0 1 1 3 1 4375 6 154296875
result:
wrong answer Jury found better answer than participant's 3 < 6 (Testcase 4)