QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#747857#9622. 有限小数wei177WA 118ms3720kbC++172.1kb2024-11-14 18:30:142024-11-14 18:30:14

Judging History

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

  • [2024-11-14 18:30:14]
  • 评测
  • 测评结果:WA
  • 用时:118ms
  • 内存:3720kb
  • [2024-11-14 18:30:14]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define N 200005
typedef long long ll;
const ll e9 = 1000000000;
vector<ll> rec;
ll gcd(ll a, ll b) {
    if (b == 0)
        return a;
    return gcd(b, a % b);
}

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

ll cal(ll a, ll b, ll c)
{
    ll x, y;
    ll gcd = exgcd(a, b, x, y);
    if (c % gcd != 0) return -1;
    x *= c / gcd;//转化为a*x+b*y=c的解
    b /= gcd;//约去c后原来b就变为了b/gcd;
    if (b < 0) b = -b;//如果b为负数就去绝对值
    ll ans = x % b;
    if (ans <= 0) ans += b;//求最小正整数解
    return ans;//返回的就是最小正整数解
}

void solve() {
    ll a, b;
    cin >> a >> b;
    ll w = b;
    while (w % 2 == 0)
        w /= 2;
    while (w % 5 == 0)
        w /= 5;
    ll minc = e9, ansd = e9;
    for (auto e : rec)
    {
        ll c, k;
        //aew = ww * k + b(-c)
        //ae = w * k + eb(-c)
        ll eb = b / w;
        k = cal(w, eb, a * e);
        ll de = (a * e - w * k) / w;
        if ((a * e - w * k) % w)
            de++;
        if (de > 0)
            k += de;
        ll d = w * e;
        c = (w * k - a * e) / eb;
        ll g_cd = gcd(c, d);
        c /= g_cd;
        d /= g_cd;
        if (c < minc && d <= e9) {
            minc = c;
            ansd = d;
        }
        else if (c == minc && d < ansd) {
            minc = c;
            ansd = d;
        }
    }
    cout << minc << " " << ansd << "\n";
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int t = 1;
    cin >> t;
    ll x1 = 1;
    for (int i = 1; i <= 32; i++) {
        ll x2 = 1;
        for (int j = 1; j <= 20; j++){
            if (x1 > e9 || x2 > e9)
                continue;
            if (x1 * x2 > e9)
                continue;
            rec.push_back(x1 * x2);
            x2 *= 5;
        }
        x1 *= 2;
    }
    while(t--){
        solve();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3648kb

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: 118ms
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:

0 1
1 54272
0 1
0 1
1 231680000
23 3936
1 36096000
5 326
1 63360
0 1
1 31232
0 1
1 4880
0 1
0 1
1 11714560
1 331250
1 2944
1 31
0 1
1 289600000
1 455000
1 58880
1 51840
0 1
0 1
0 1
1 415
1 19328000
1 765000000
0 1
1 608
1 72192
0 1
0 1
3 347500
1 944
1 43600
0 1
1 430000
1 6336
1 29184
11 795000
0 1...

result:

wrong answer The result is not terminating.(Testcase 1)