QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#628614 | #6440. Xingqiu's Joke | qstbcd | TL | 0ms | 4528kb | C++23 | 1.2kb | 2024-10-10 21:14:17 | 2024-10-10 21:14:17 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 5;
const int mod = 1e9;
int __t = 1, n, a, b, tag[N];
vector<int> prime;
unordered_map<int, int> mp;
int dfs(int x, int y) {
int z = x - y, xy = x * mod + y;
if (x == 1 || y == 1)
return mp[xy] = 0;
if (mp.find(xy) != mp.end())
return mp[xy];
if (z == 1)
return mp[xy] = y - 1;
int ans = y - 1;
for (auto p : prime) {
if (z < p)
break;
if (z % p == 0) {
int xp1 = x % p, xp2 = p - xp1;
ans = min(ans, dfs((x - xp1) / p, (y - xp1) / p) + 1 + xp1);
ans = min(ans, dfs((x + xp2) / p, (y + xp2) / p) + 1 + xp2);
}
}
return mp[xy] = ans;
}
void solve() {
cin >> a >> b;
if (a < b)
swap(a, b);
dfs(a, b);
cout << mp[a * mod + b] << "\n";
return;
}
int32_t main() {
#ifdef ONLINE_JUDGE
ios::sync_with_stdio(false);
cin.tie(0);
#endif
for (int i = 2; i <= 1e5; i++) {
if (tag[i])
continue;
prime.push_back(i);
for (int j = i * i; j <= 1e5; j += i)
tag[j] = 1;
}
cin >> __t;
while (__t--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 4528kb
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...