QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#231839 | #6440. Xingqiu's Joke | sundage# | WA | 75ms | 3536kb | C++20 | 1.7kb | 2023-10-29 17:03:38 | 2023-10-29 17:03:38 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve() {
int a1, b1;
cin >> a1 >> b1;
if (a1 > b1) {
swap(a1, b1);
}
int t = b1 - a1;
vector<int>cnt;
int z = t;
for (int i = 2; i * i <= z; i++) {
if (t % i == 0) {
cnt.push_back(i);
while (t % i == 0) {
t /= i;
}
}
}
if (t != 1) {
cnt.push_back(t);
}
// for (auto x : cnt) {
// cout << x << endl;
// }
map<pair<int, int>, int>mp;
mp[ {a1, b1}] = a1 - 1;
int ans = a1 - 1;
function<void(int, int, int)>dfs1 = [&](int a, int b, int tt) {
if (tt >= ans) {
return;
}
if (a == 1) {
return;
}
if (mp[ {a, b}] && a != a1) {
return;
}
if (mp[ {a, b}] == 0)
mp[ {a, b}] = a - 1;
for (int i = 0; i < cnt.size(); i++) {
int x = a % cnt[i];
if (a % cnt[i] != b % cnt[i]) {
continue;
}
int u;
if (x == a) {
u = x - 1;
ans = min(ans, tt + x - 1);
} else {
dfs1((a - x) / cnt[i], (b - x) / cnt[i], tt + x + 1);
u = x + 1 + mp[ {(a - x) / cnt[i], (b - x) / cnt[i]}];
}
// cout << u << " 21" << endl;
ans = min(ans, u + tt);
dfs1(((cnt[i] - x) + a) / cnt[i], ((cnt[i] - x) + b) / cnt[i], tt + cnt[i] - x + 1 );
u = min(u, mp[ {((cnt[i] - x) + a) / cnt[i], ((cnt[i] - x) + b) / cnt[i]}] + cnt[i] - x + 1);
mp[ {a, b}] = min(mp[ {a, b}], u);
ans = min(ans, u + tt);
}
// cout << a << " " << b << " " << tt << " " << mp[ {a, b}] << endl;
return ;
};
dfs1(a1, b1, 0);
// cout << ans << endl;
cout << mp[ {a1, b1}] << endl;
return;
}
//5156 6326
signed main() {
ios::sync_with_stdio(false);
int tt = 1;
cin >> tt;
while (tt--) {
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3392kb
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: 75ms
memory: 3536kb
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:
4 6 5 7 6 6 6 7 6 5 6 6 7 5 6 7 5 6 6 6 7 5 7 5 6 6 5 7 9 6 7 7 6 4 7 5 5 6 7 5 6 6 5 6 6 7 6 6 6 5 5 6 6 7 5 5 6 4 5 7 4 8 4 5 6 6 7 7 5 4 6 6 6 5 5 6 6 6 5 5 6 5 6 7 7 5 5 5 5 6 5 5 7 6 6 5 6 5 7 6 6 6 6 6 8 7 6 6 6 7 4 6 6 7 6 5 6 5 5 5 7 5 6 6 5 4 7 5 5 6 6 6 4 7 7 5 6 6 6 6 5 6 5 6 8 7 7 5 6 6 ...
result:
wrong answer 1st numbers differ - expected: '12', found: '4'