QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#690143#6440. Xingqiu's JokeVegetogWA 361ms12716kbC++202.0kb2024-10-30 20:31:132024-10-30 20:31:14

Judging History

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

  • [2024-10-30 20:31:14]
  • 评测
  • 测评结果:WA
  • 用时:361ms
  • 内存:12716kb
  • [2024-10-30 20:31:13]
  • 提交

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 {
        // cout << a << " " << b << endl;
        if (a == 1 || b == 1) return 0;
        if (mp.count({a, b})) return mp[{a, b}];
        if (mp.count({b, a})) return mp[{b, a}];

        int res = 0;
        int cnt = INF;
        for (int e : fac) {
            if (a % e == b % e) {
                if (a % e <= e - a % e) {
                    cnt = a % e;
                    res = e;
                } else {
                    cnt = e - a % e;
                    res = e;
                }
                if (mp.count({a, b})) {
                    mp[{a, b}] =
                        min(mp[{a, b}], 1 + cnt + dfs(a / res, b / res));
                } else {
                    mp[{a, b}] = 1 + cnt + dfs(a / res, b / 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[{2,6}]<<"\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();
    }
}

詳細信息

Test #1:

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

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: 361ms
memory: 12716kb

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
18
14
15
14
16
12
18
14
17
14
14
18
16
16
18
14
17
14
16
16
16
17
13
15
15
14
12
17
14
12
18
14
15
12
16
17
15
14
13
14
18
14
15
13
14
16
18
17
13
14
15
15
17
14
15
15
13
17
15
13
17
18
16
16
14
15
14
22
13
15
17
18
17
14
15
15
17
10
16
18
15
13
18
16
14
18
13
15
15
15
13
17
17
14
17
15
12
12
14
...

result:

wrong answer 2nd numbers differ - expected: '19', found: '18'