QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#697162#6440. Xingqiu's Jokefsy_juruo#TL 0ms3612kbC++171.7kb2024-11-01 11:30:572024-11-01 11:30:57

Judging History

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

  • [2024-11-01 11:30:57]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3612kb
  • [2024-11-01 11:30:57]
  • 提交

answer

#include <bits/stdc++.h> 
using LL = long long;

int T, a, b;

std::vector<std::pair<int, int> > fac;
void factor(int x) {
    fac.clear();
    for(int i = 2; i <= sqrt(x); i++) {
        int cnt = 0;
        while(x % i == 0) {
            x /= i;
            ++cnt;
        }
        if(cnt) fac.emplace_back(std::make_pair(i, cnt));
    }
    if(x > 1) fac.emplace_back(std::make_pair(x, 1));
}

int dfs(int x, int y) {
    if(x == 0) return 1;
    if(x == 1) return 0;
    int ans = x - 1;
    for(auto p : fac) {
        if((y - x) % p.first == 0) {
         //   std::cout << x << " " << y << " " << p.first << " " << ans << "\n";
            if(y % p.first <= p.first - y % p.first) {
                int v = y / p.first * p.first;
                int u = x / p.first * p.first;
                int cc = 0;
                //while(u > 0 && u % p.first == 0) ++cc, 
                u /= p.first, v /= p.first;
                ans = std::min(ans, y % p.first + 1 + dfs(u, v));
            } else {
                int v = (y / p.first + 1) * p.first;
                int u = (x / p.first + 1) * p.first;
                int cc = 0;
                //while(u > 0 && u % p.first == 0) ++cc, 
                u /= p.first, v /= p.first;
                ans = std::min(ans, p.first - y % p.first + 1 + dfs(u, v));
            }
        }
    } 
    return ans;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cin >> T;
    while(T--) {
        std::cin >> a >> b;
        if(a > b) std::swap(a, b);

        if(b - a == 1) std::cout << a - 1 << "\n";
        else {
            factor(b - a);
            std::cout << dfs(a, b) << "\n";
        }
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: