QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#716578#6440. Xingqiu's Jokejty233TL 0ms3616kbC++231.6kb2024-11-06 15:35:322024-11-06 15:35:33

Judging History

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

  • [2024-11-06 15:35:33]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3616kb
  • [2024-11-06 15:35:32]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using i64 = long long;
#define int long long

unordered_map<int,int> dp2;
int check(int x) {
    if (dp2.count(x))
        return dp2[x];
    int ans = 0ll;
    for(int i = 2; i * i <= x; i++) {
        while(x % i == 0) {
            ans++;
            x /= i;
        }
    }
    if(x > 1) ans++;
    dp2[x] = ans;
    return ans;
}

int c;

map<pair<int,int>, int> dp;

int dfs(int x,int f)
{
    if (x == 1)
        return 0;

    if (dp.count({x, f}))
        return dp[{x, f}];
    
    int ans = 1e9;
    int d = c / f;

    if (d == 1)
        return 1e9;


    auto fun = [&](int u)
    {
        if (u > x)
            return (int)(1e9);
        int t = x % u;
        int t2 = u - t;
        return check(u) + min(dfs((x - t) / u,f * u) + t, dfs((x + t2) / u, f * u) + t2);
    };
    for (int i = 2;i * i <= d;i++)
    {
        if (d % i == 0)
        {
            ans = min({fun(i), fun(d / i), ans});
        }
    }
    ans = min(ans, fun(d));

    dp[{x, f}] = ans;

    return ans;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t;
    cin >> t;

    while (t--){
        int a, b;
        cin >> a >> b;
        if(a > b) swap(a, b);
        c = b - a;

        if(c == 0) {
            throw std::runtime_error("humorous data");
        }
        if(c == 1){
            cout << a - 1 << '\n';
            continue;
        }

        dp.clear();

        cout << dfs(a, 1) << endl;
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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:

14
1000000000
19
22
1000000000
24
18
1000000000
16
1000000000
19
18
20
25
1000000000
1000000000
17
21
17
1000000000
1000000000
1000000000
25
24
17
1000000000
22
14
1000000000
23
14
1000000000
25
1000000000
16
1000000000
1000000000
22
22
20
16
1000000000
17
32
23
16
1000000000
1000000000
1000000000
1...

result: