QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#661814#6440. Xingqiu's Jokewxy3265WA 437ms4684kbC++141.9kb2024-10-20 18:19:462024-10-20 18:19:48

Judging History

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

  • [2024-10-20 18:19:48]
  • 评测
  • 测评结果:WA
  • 用时:437ms
  • 内存:4684kb
  • [2024-10-20 18:19:46]
  • 提交

answer

//
// Created by wxy3265 on 2024/10/20.
//

#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#define int long long
using namespace std;

struct Status{
    int a, b, step;
};

const int MAXP = 1e5 + 3;
int v[MAXP], prime[MAXP];
int cnt;
void primes(int n) {
    cnt = 0;
    for (int i = 2; i <= n; i++) {
        if (!v[i]) {
            v[i] = i;
            prime[++cnt] = i;
        }
        for (int j = 1; j <= cnt; j++) {
            if (prime[j] > v[i] || prime[j] > n / i) break;
            v[i * prime[j]] = prime[j];
        }
    }
}

int d[MAXP];
int tot;
map<pair<int, int>, int> mp;

void dfs(int a, int b) {
    if (mp.count(make_pair(a, b))) return;
    if (a - b == 1) {
        mp[make_pair(a,b)] = b - 1;
        return;
    }
    if (b == 1) {
        mp[make_pair(a, b)] = 0;
        return;
    }
    mp[make_pair(a, b)] = 1e18;
    for (int i = 1; i <= tot; i++) {
        int p = d[i];
        if ((a - b) % p == 0) {
            int step1 = b % p;
            int step2 = p - step1;
            dfs(a / p, b / p);
            mp[make_pair(a, b)] = min(mp[make_pair(a, b)], mp[make_pair(a / p, b / p)] + step1 + 1);
            int ba = ceil((double)a / (double) p), bb = ceil((double)b / (double) p);
            dfs(ba,  bb);
            mp[make_pair(a, b)] = min(mp[make_pair(a, b)], mp[make_pair(ba, bb)] + step2 + 1);
        }
    }
}

signed main() {
    ios::sync_with_stdio(false);   cin.tie(nullptr), cout.tie(nullptr);
    primes(1e5);
    int t;
    cin >> t;
    while (t--) {
        tot = 0;
        mp.clear();
        int x, y;
        cin >> x >> y;
        if (x < y) swap(x, y);
        int del = x - y;
        for (int i = 1; i <= cnt && prime[i] <= del; i++) {
            if (del % prime[i] == 0) d[++tot] = prime[i];
        }
        dfs(x, y);
        cout << mp[make_pair(x, y)] << '\n';
    }
    return 0;
}

詳細信息

Test #1:

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

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: 437ms
memory: 4672kb

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
16
18
17
17
19
15
18
15
16
17
18
19
15
17
17
17
13
19
16
13
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
16
18
12
15
18
16
13
19
18
16
18
17
15
13
16
...

result:

wrong answer 12th numbers differ - expected: '15', found: '16'