QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#717304#6440. Xingqiu's JoketieguodundaeWA 484ms4552kbC++232.5kb2024-11-06 17:32:552024-11-06 17:32:55

Judging History

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

  • [2024-11-06 17:32:55]
  • 评测
  • 测评结果:WA
  • 用时:484ms
  • 内存:4552kb
  • [2024-11-06 17:32:55]
  • 提交

answer

#include <bits/stdc++.h>
#define pii pair<int, int>
#define fi first
#define se second
using namespace std;
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", print_args(__VA_ARGS__)
template <class... T>
void print_args(T... args) { ((cerr << args << ' '), ...) << '\n'; }
typedef long long ll;
const int mod = 998244353;
const int inf = 1e9;
const ll INF = 1e18;
const int N = 2E5 + 10;
//a%x=0,b%x=0->(a-b)%x=0
//前两种操作a-b不变
//a-b的质因子固定不变
//第三种操作其实就是不断/因子x的过程
//最优结果肯定是先调整ab使%x=0,然后/x
//1e9的数的质因子>sqrt(n)的最多一个,可以暴力找
int prime[N], vis[N], idx;
map<pii, int> dp;//记忆化
vector<int> g;
int a, b;
void ini() {
    for (int i = 2; i < N; i++) {
        if (!vis[i]) prime[idx++] = i;
        for (int j = 0; j < idx && prime[j] * i < N; j++) {
            vis[prime[j] * i] = 1;
            if (i % prime[j] == 0) break;
        }
    }
}
void get_prime(int x) {
    int res = x;
    for (int i = 0; i < idx; i++) {
        int y = prime[i];
        if (y > res) break;
        if (x % y == 0) g.push_back(y);
        while (x % y == 0) {
            x /= y;
        }
    }
    if (x > 1) g.push_back(x);
}
int dfs(int a, int d) {//复杂度d*2,a受d牵连,状态只有两个
    if (a == 1) return 0;
    if (d == 1) return a - 1;//互质,只能一个一个减
    if (dp[{a, d}] != 0) return dp[{a, d}];
    for (auto x : g) {
        if (x > d) break;
        if (d % x) continue;
        // int ce = a / x + (a % x != 0);//上取整,使a%x=0,(b-a)%x=0
        // int cnt_ce = (a % x == 0 ? 0 : x - (a % x));
        // int fl = a / x;//下取整
        // int cnt_fl = (a % x == 0 ? 0 : a % x);
        // int mn = min(dfs(ce, d / x) + cnt_ce, dfs(fl, d / x) + cnt_fl);
        int mn = min(dfs(a / x + 1, d / x) + x - a % x, dfs(a / x, d / x) + a % x);
        if (dp[{a, d}] == 0) dp[{a, d}] = mn + 1;
        else dp[{a, d}] = min(dp[{a, d}], mn + 1);
    }
    return min(a - 1, dp[{a, d}]);
}
void _() {
    if (a > b) swap(a, b);
    get_prime(b - a);
    cout << dfs(a, b - a) << '\n';//b = a + (b - a)
}
void input() {
    cin >> a >> b;
    g.clear();
    dp.clear();
}
void sol();
signed main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    ini();

    int _ = 1;
    cin >> _;
    while (_--) {
        sol();
    }
    return 0;
}
void sol() {
    input(); _();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 4404kb

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: 484ms
memory: 4552kb

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
15
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 17th numbers differ - expected: '14', found: '15'