QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#687671#6440. Xingqiu's JokePalbudirRE 78ms13048kbC++202.2kb2024-10-29 20:22:352024-10-29 20:22:36

Judging History

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

  • [2024-10-29 20:22:36]
  • 评测
  • 测评结果:RE
  • 用时:78ms
  • 内存:13048kb
  • [2024-10-29 20:22:35]
  • 提交

answer

#include <bits/stdc++.h>
#include <unordered_map>
using i80 = __int128;
using i64 = long long;
using f80 = long double;
constexpr i64 p = 2e9 + 1;
constexpr int N = 1e3;
std::vector<int> pm, isp(N);
std::unordered_map<i64, int> map;
void work() {
    auto chmin = [](int& x, int y) {
        if (y < x) {
            x = y;
        }
    };
    int a, b, c;
    std::cin >> a >> b;
    if (a > b) {
        std::swap(a, b);
    }
    c = b - a;
    std::vector<std::array<int, 2>> d;
    for (int i = 1; pm[i] * pm[i] <= c; ++i) {
        if (c % pm[i]) {
            continue;
        }
        int k = 0;
        while (c % pm[i] == 0) {
            ++k;
            c /= pm[i];
        }
        d.push_back({pm[i], k});
    }
    if (c > 1) {
        d.push_back({c, 1});
    }
    int n = d.size();
    std::function<int(int, int)> dfs = [&](int x, int y) {
        if (x == 1) {
            return 0;
        }
        if (map.count(x * p + y)) {
            return map[x * p + y];
        }
        int ans = x - 1;
        for (int i = 0; i < n; ++i) {
            if (d[i][1] == 0) {
                continue;
            }
            int k = x % d[i][0];
            --d[i][1];
            chmin(ans, dfs((x + k) / d[i][0], (y + k) / d[i][0]) + d[i][0] - k + 1);
            chmin(ans, dfs((x - k) / d[i][0], (y - k) / d[i][0]) + k + 1);
            ++d[i][1];
        }
        // for (int i = 0; i < n; ++i) {
        //     std::cout << "{" << d[i][0] << "," << d[i][1] << "}";
        // }
        // std::cout << x << "," << y << ":" << ans << "\n";
        return map[x * p + y] = ans;
    };
    std::cout << dfs(a, b) << "\n";
    return;
}
void init() {
    pm = {1};
    for (int i = 2; i < N; ++i) {
        if (!isp[i]) {
            pm.push_back(i);
        }
        for (int j = 1; j < (int)pm.size() && i * pm[j] < N; ++j) {
            isp[i * pm[j]] = true;
            if (i % pm[j] == 0) {
                break;
            }
        }
    }
}
int main() {
    init();
    std::ios::sync_with_stdio(false);
    // std::cin.tie(nullptr), std::cout.tie(nullptr);
    int t = 1;
    std::cin >> t;
    while (t--) {
        work();
    }
    return 0;
}
/*


*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 0
Accepted
time: 78ms
memory: 13048kb

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
17
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:

ok 300 numbers

Test #3:

score: 0
Accepted
time: 0ms
memory: 4228kb

input:

300
636099362 669653794
640628976 607074544
492031079 458476647
420324662 386770230
234845232 201290800
416098756 382544324
513320531 546874963
245050186 278604618
688926901 722481333
458533405 424978973
863331691 896886123
638892374 672446806
849746534 816192102
761591596 728037164
369934525 403488...

output:

51
49
48
45
35
45
50
42
53
47
61
53
59
55
43
41
53
55
44
62
48
39
36
60
40
55
49
54
50
38
59
41
39
43
47
57
54
50
52
39
54
52
39
37
32
34
56
38
53
41
51
50
55
56
35
44
35
45
59
33
53
59
56
57
43
53
54
45
34
58
33
55
56
33
46
49
33
38
38
55
54
47
56
47
52
60
36
53
56
53
44
43
34
59
55
49
52
40
35
55
...

result:

ok 300 numbers

Test #4:

score: -100
Runtime Error

input:

300
867787179 877654074
403881553 407432589
201172453 192754452
822066929 815320907
516801596 518734008
278735158 276997924
39842929 46406996
229923235 222281597
958111516 962220928
514454467 511546909
66273896 56535006
603599579 602886043
99119397 89783150
606236932 603774684
948362977 945928285
92...

output:

33397
48909

result: