QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#740409#9622. 有限小数mobbbTL 0ms3536kbC++201.9kb2024-11-13 09:47:172024-11-13 09:47:32

Judging History

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

  • [2024-11-13 09:47:32]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3536kb
  • [2024-11-13 09:47:17]
  • 提交

answer

#include <bits/stdc++.h>

#define ll long long


ll exgcd(ll a, ll b, ll &x, ll &y){
    if (b == 0){
        x = 1, y = 0;
        return a;
    }
    int d = exgcd(b, a % b, y, x);
    y -= a / b * x;
    return d;
}

void solve(){
    int a, b;

    std::cin >> a >> b;
    std::vector<ll> num;

    if (b == 5 || b == 2){
        std::cout << "0 1\n";
        return;
    }

    constexpr int MAX = 1e6;

    for (ll i = 1; i <= MAX; i *= 2){
        for (ll j = 1; i * j <= MAX; j *= 5){
            for (ll k = 1; i * k * j <= MAX; k *= b){
                num.push_back(i * k * j);
            }
        }
    }

    std::sort(num.begin(), num.end());
    std::pair<ll, ll> ans = {1e18, 1e18};
    for (auto d : num){
        // std::cout << d << '\n';
        ll num1 = a * d;
        if (num1 % b == 0){
            ll by = b;
            ll t = d;

            while (t % b == 0){
                by *= b;
                t /= b;
            }
            
            auto get = [](ll a, ll b, ll m){
                ll x, y;
                ll d = exgcd(a, b, x, y);
                if (m % d){
                    return -1ll;
                }
                y = -y;
                while (x < 0 || y < 0) x += b / d, y += a / d;
                while (x >= b / d && y >= a / d) x -= b / d, y -= a / d;
                return x;
            };

            ll x = get(by, b, num1);
            if (x == -1) continue;
            by = x * by;
            by -= num1;
            by /= b;
            if (ans > std::make_pair(by, d) && by >= 0){
                ans = std::make_pair(by, d);
            }
        }
    }

    std::cout << ans.first << ' ' << ans.second << '\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: 0ms
memory: 3536kb

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
Time Limit Exceeded

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:


result: