QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#676790 | #6440. Xingqiu's Joke | RPChe_ | TL | 0ms | 3740kb | C++20 | 1.4kb | 2024-10-26 00:43:47 | 2024-10-26 00:43:48 |
Judging History
answer
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
const int N = 55;
int a, b;
vector<int> v[N];
void factor(int n) {
v[1].clear();
int lim = sqrt(n);
for (int i = 2; i <= lim; i++) {
if (!(n % i)) {
v[1].push_back(i);
while (!(n % i)) n /= i;
}
}
if (n != 1) v[1].push_back(n);
}
int calc(int dep, int a, int b) {
if (a == 1) return 0;
int d = b - a, res = a - 1;
if (res == 1) return res;
v[dep + 1].clear();
for (int p : v[dep]) {
if (!(d % p)) {
v[dep + 1].push_back(p);
}
}
for (int p : v[dep + 1]) {
if (!(a % p)) {
res = min(res, 1 + calc(dep + 1, a / p, b / p));
} else {
if (a > p) {
int t = a - a / p * p;
if (t + 1 < res) res = min(res, t + 1 + calc(dep + 1, (a - t) / p, (b - t) / p));
}
int t = (a / p + 1) * p - a;
if (t + 1 < res) res = min(res, t + 1 + calc(dep + 1, (a + t) / p, (b + t) / p));
}
}
return res;
}
int main() {
int T; scanf("%d", &T);
while (T--) {
scanf("%d%d", &a, &b);
if (a > b) swap(a, b);
if (a == 1) printf("0\n");
else if (a == b) printf("1\n");
else {
factor(b - a);
printf("%d\n", calc(1, a, b));
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3740kb
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...