QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#800643#9622. 有限小数CCSU_YZTWA 0ms3728kbC++201.7kb2024-12-06 13:41:422024-12-06 13:41:43

Judging History

This is the latest submission verdict.

  • [2024-12-06 13:41:43]
  • Judged
  • Verdict: WA
  • Time: 0ms
  • Memory: 3728kb
  • [2024-12-06 13:41:42]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;

// 扩展欧几里得算法求逆元:在已知gcd(a,m)=1的情况下求a在模m下的逆元
long long extended_gcd(long long a, long long b, long long &x, long long &y) {
    if (b == 0) {
        x = 1; y = 0;
        return a;
    }
    long long x1, y1;
    long long g = extended_gcd(b, a % b, x1, y1);
    x = y1;
    y = x1 - (a / b)*y1;
    return g;
}

long long modInverse(long long a, long long m) {
    long long x, y;
    long long g = extended_gcd(a, m, x, y);
    // 假设g=1
    x = (x % m + m) % m;
    return x;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int T; cin >> T;
    while(T--){
        long long a,b; cin >> a >> b;

        // 因为gcd(a,b)=1
        // 将b分解为q*s, q只包含2和5
        // 提取b中的2因子
        long long bb = b;
        long long q = 1;
        while (bb % 2 == 0) { q *= 2; bb /= 2; }
        while (bb % 5 == 0) { q *= 5; bb /= 5; }

        long long s = bb; // s为剩余因子部分

        if (s == 1) {
            // b本身只含2和5,a/b是有限小数
            // 输出c=0,d=1即可
            cout << 0 << " " << 1 << "\n";
        } else {
            // 我们选择d = s
            // 要求 s | (a + c*q)
            // 即 a + c*q ≡ 0 (mod s)
            // c*q ≡ -a (mod s)
            // 由于gcd(q,s)=1,可以求q的逆元
            long long c;
            long long q_inv = modInverse(q, s);
            long long val = ((-a) % s + s) % s; 
            c = (val * q_inv) % s; // 最小非负解

            cout << c << " " << s << "\n";
        }
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3728kb

input:

4
1 2
2 3
3 7
19 79

output:

0 1
1 3
4 7
60 79

result:

wrong answer Jury found better answer than participant's 1 < 4 (Testcase 3)