QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#746767#9622. 有限小数YurilyRE 0ms0kbC++202.3kb2024-11-14 15:29:192024-11-14 15:29:21

Judging History

你现在查看的是最新测评结果

  • [2024-11-14 15:29:21]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-11-14 15:29:19]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
//#define LL __int128
using namespace std;

typedef long long LL;
const LL e9 = 1e9;
const LL e16 = 1e15;

    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;
}

LL qpow(LL a, LL b){
    LL ret = 1;
    while(b){
        if(b & 1)
            ret = ret * a;
        a = a * a;
        b >>= 1;
    }
    return ret;
}

void sol(){
    long long tmpa,tmpb;
    scanf("%lld%lld", &tmpa, &tmpb);
    a=tmpa,b=tmpb;

    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 = e16, mind = e16;
    LL x, y, t, c, d, g;

    LL xi, yi;
    for(xi = 0, x = 1; k * x <= e16; xi++, x *= 2){
        for(yi = 0, y = 1; k * x * y <= e16; yi++, y *= 5){
            // if(xi == 1LL && yi == 0LL)
            //     printf("xi=%lld yi=%lld\n", xi, yi);

            LL p = qpow(2, b2) * qpow(5, b5);
            LL q = k - (a * x % k * y % k);
            //p * x1 + k * y1 === q
            //t = min(x1)
            t = getf(p, k, q);
            c = t;
            d = k * x * y;
            g = gcd(c, d);
            c /= g;
            d /= g;
            if(c < minc && d <= e9){
                minc = c;
                mind = d;
            }
        }
    }

    printf("%lld %lld\n", (long long)minc, (long long)mind);

}

int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);

    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
Dangerous Syscalls

input:

4
1 2
2 3
3 7
19 79

output:


result: