QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#584910 | #6440. Xingqiu's Joke | rxzfn639 | WA | 96ms | 3632kb | C++23 | 3.1kb | 2024-09-23 17:32:37 | 2024-09-23 17:32:37 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const i64 P = 998244353;
const i64 B = 1e9 + 7;
i64 mul(i64 a, i64 b, i64 m) {
return static_cast<__int128>(a) * b % m;
}
i64 power(i64 a, i64 b, i64 m) {
i64 res = 1 % m;
for (; b; b >>= 1, a = mul(a, a, m))
if (b & 1)
res = mul(res, a, m);
return res;
}
bool isprime(i64 n) { // Miller-Rabin
if (n < 2) return false;
static constexpr int B[] = {2, 3, 5, 7, 11, 13, 17, 19, 23};
for (int p : B) {
if (n == p) return true;
if (n % p == 0) return false;
}
i64 m = (n - 1) >> __builtin_ctz(n - 1);
for (int p : B) {
i64 t = m, a = power(p, m, n);
while (t != n - 1 && a != 1 && a != n - 1) {
a = mul(a, a, n);
t *= 2;
}
if (a != n - 1 && t % 2 == 0) return false;
}
return true;
}
vector<i64> factorize(i64 n) { // Pollard-Rho
vector<i64> p;
function<void(i64)> f = [&](i64 n) {
if (n <= 10000) {
for (int i = 2; i * i <= n; i++)
for ( ; n % i == 0; n /= i)
p.push_back(i);
if (n > 1)
p.push_back(n);
return;
}
if (isprime(n)) {
p.push_back(n);
return;
}
auto g = [&](i64 x) {
return (mul(x, x, n) + 1) % n;
};
i64 x0 = 2;
while (true) {
i64 x = x0, y = x0;
i64 d = 1, pow = 1, lam = 0, v = 1;
while (d == 1) {
y = g(y);
++lam;
v = mul(v, abs(x - y), n);
if (lam % 127 == 0) {
d = gcd(v, n);
v = 1;
}
if (pow == lam) {
x = y;
pow *= 2;
lam = 0;
d = gcd(v, n);
v = 1;
}
}
if (d != n) {
f(d);
f(n / d);
return;
}
++x0;
}
};
f(n);
sort(p.begin(), p.end());
p.erase(unique(p.begin(), p.end()), p.end());
return p;
}
void solve() {
i64 a, b;
cin >> a >> b;
if (a > b) swap(a, b);
b -= a;
auto p = factorize(b);
unordered_map<i64, i64> mp;
auto dfs = [&](auto self, i64 x, i64 y) -> i64 {
if (x == 1) return 0;
if (y == 1) return x - 1;
if (mp.count(x * B + y)) {
return mp[x * B + y];
}
i64 res = 1e9;
for (auto it : p) {
if (y % it == 0) {
res = min(res, self(self, x / it, y / it) + x % it + 1);
res = min(res, self(self, x / it + 1, y / it) + it - x % it + 1);
}
}
return mp[x * B + y] = res;
};
cout << dfs(dfs, a, b) << '\n';
}
int main() {
ios::sync_with_stdio(0); cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while(t--) solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3516kb
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: 96ms
memory: 3632kb
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 16 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 12th numbers differ - expected: '15', found: '16'