QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#716901#6440. Xingqiu's Jokejty233WA 337ms3704kbC++231.6kb2024-11-06 16:18:432024-11-06 16:18:44

Judging History

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

  • [2024-11-06 16:18:44]
  • 评测
  • 测评结果:WA
  • 用时:337ms
  • 内存:3704kb
  • [2024-11-06 16:18:43]
  • 提交

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), ans});

            while (d % i == 0)
                d /= i;
        }
    }
    if (d > 1)
        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 << endl;
            continue;
        }

        dp.clear();

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

    return 0;
}

詳細信息

Test #1:

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

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: 337ms
memory: 3704kb

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
1000000000
15
17
1000000000
17
13
1000000000
15
1000000000
15
17
18
20
1000000000
1000000000
16
18
15
1000000000
1000000000
1000000000
19
17
17
1000000000
17
13
1000000000
17
13
1000000000
21
1000000000
13
1000000000
1000000000
18
18
15
14
1000000000
14
24
16
14
1000000000
1000000000
1000000000
1...

result:

wrong answer 2nd numbers differ - expected: '19', found: '1000000000'