QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#690189#6440. Xingqiu's JokeVegetogWA 400ms13440kbC++202.3kb2024-10-30 20:52:062024-10-30 20:52:06

Judging History

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

  • [2024-10-30 20:52:06]
  • 评测
  • 测评结果:WA
  • 用时:400ms
  • 内存:13440kb
  • [2024-10-30 20:52:06]
  • 提交

answer

#include <bits/stdc++.h>
#ifndef ONLINE_JUDGE
#include "_my_utils.h"
#endif
#define int long long

using namespace std;
using ll = long long;
using PII = std::pair<int, int>;

constexpr ll INF = 2E18 + 10;
constexpr int N = 2E6 + 10;

map<PII, int> mp;

void SINGLE_TEST() {
    int ca, cb;
    cin >> ca >> cb;

    int c = abs(ca - cb);

    vector<int> fac;
    int oldc = c;
    for (int i = 2; i * i <= oldc; i++) {
        if (c % i == 0) {
            fac.push_back(i);
            // if (i * i != oldc) fac.push_back(oldc / i);
            while (c % i == 0) c /= i;
        }
    }
    if (c != 1) fac.push_back(c);
    sort(fac.begin(), fac.end(), greater<>());
    // debug(fac);
    function<int(int, int)> dfs = [&](int a, int b) -> int {
        if (a > b) swap(a, b);
        // cout << a << " " << b << endl;
        if (a == 1) return 0;
        if(mp.count({a, b})) return mp[{a, b}];

        int res = 0;
        int cnt = INF;
        for (int e : fac) {
            int ra, rb;
            if (a % e == b % e) {
                if (a % e <= e - a % e) {
                    cnt = a % e;
                    res = e;
                    ra = a - cnt;
                    rb = b - cnt;
                } else {
                    cnt = e - a % e;
                    res = e;
                    ra = a + cnt;
                    rb = b + cnt;
                }
                // COUT(cnt);
                // COUT(res);
                // cout<<"\n";
                if (mp.count({a, b})) {
                    mp[{a, b}] =
                        min(mp[{a, b}], 1 + cnt + dfs(ra / res, rb / res));
                } else {
                    mp[{a, b}] = 1 + cnt + dfs(ra / res, rb / res);
                }
            }
        }
        if (mp.count({a, b})) {
            mp[{a, b}] = min(mp[{a, b}], min(a, b) - 1);
        } else {
            mp[{a, b}] = min(a, b) - 1;
        }
        return mp[{a, b}];
    };
    // cout<<mp[{1,2}]<<"*\n";
    cout << dfs(ca, cb) << "\n";
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);

    int SAMPLES = 1;
    cin >> SAMPLES;
    for (int CUR = 1; CUR <= SAMPLES; CUR++) {
        SINGLE_TEST();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

5
4 7
9 8
32 84
11 35
2 1

output:

2
7
5
4
0

result:

ok 5 number(s): "2 7 5 4 0"

Test #2:

score: -100
Wrong Answer
time: 400ms
memory: 13440kb

input:

300
223528529 446621399
755625805 978718675
257717538 480810408
277875777 54782907
963091558 739998688
282469431 505562301
38752451 261845321
474429597 697522467
67026335 290119205
874629705 651536835
301964126 78871256
316864911 93772041
545942587 322849717
640801578 417708708
683070559 906163429
9...

output:

12
19
15
17
16
17
13
19
15
17
15
15
18
17
17
19
14
18
15
16
17
18
19
15
17
17
17
12
19
16
12
19
16
15
13
18
17
18
16
15
14
17
14
16
15
14
19
18
16
14
16
17
16
18
14
16
17
14
18
18
16
19
19
18
17
16
16
14
23
14
14
18
18
20
16
16
17
17
12
18
19
17
16
18
16
15
18
12
15
18
16
12
19
18
15
18
17
14
13
16
...

result:

wrong answer 60th numbers differ - expected: '17', found: '18'