QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#661818#6440. Xingqiu's Jokewxy3265TL 1ms4600kbC++141.9kb2024-10-20 18:20:452024-10-20 18:20:45

Judging History

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

  • [2024-10-20 18:20:45]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:4600kb
  • [2024-10-20 18:20:45]
  • 提交

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--) {
        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: 1ms
memory: 4600kb

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
Time Limit Exceeded

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:


result: