代码是我楼下那位的,我改了一下。所以我也不知道这段代码在干嘛。
#include <bits/stdc++.h>
#define int __int128
using namespace std;
int Prime[7] = {2, 3, 5, 7, 11, 13, 37};
int gcd(int a, int b) {return b == 0 ? a : gcd(b, a % b);}
template <class T>
T randint(T l, T r = 0) {
static mt19937 eng(time(0));
if (l > r)
swap(l, r);
uniform_int_distribution<T> dis(l, r);
return dis(eng);
}
int Qpow(int a, int b, int p) {
int res = 1;
while (b) {
if (b & 1) res = res * a % p;
a = a * a % p, b >>= 1;
}
return res;
}
bool Miller_Rabin(long long n) {
if (!(n % 2) || n == 1) return n == 2;
for (int v=1; v<=10; v++) {
long long b = rand() % (n - 2) + 2, p = __builtin_ctzll(n - 1), r = Qpow(b, (n - 1) >> p, n), i; if (r == 1) continue;
for (i=1; i<=p; i++) {
if (r == n - 1) break;
r = (__int128) r * r % n;
}
if (i > p) return false;
}
return true;
}
bool isP(int x) {
return Miller_Rabin(x);
}
int f(int x, int c, int p) {return ((__int128)x * x + c) % p;}
int Pollard_Rho(int x) {
if (x == 4) return 2;
if (isP(x)) return x;
while (1) {
int c = randint <long long> (1, x - 1);
int t = 0, r = 0, p = 1, q;
do {
for (int i = 0; (t = f(t, c, x), r = f(f(r, c, x), c, x)), i < 128; ++i) {
if (t == r or (q = (__int128)p * abs((long long)(t - r)) % x) == 0) break;
p = q;
}
int d = gcd(p, x);
if (d > 1) return d;
} while (t != r);
}
}
int Find(int x) {
int fac = Pollard_Rho(x);
if (fac == x) return x;
else return max(Find(fac), Find(x / fac));
}
void solve() {
long long n; cin >> n;
int k = Find(n);
if (k == n) puts("Prime");
else cout << (long long)k << '\n';
}
signed main() {
long long T; cin >> T;
while (T--) solve();
return 0;
}
不开 O2:https://www.luogu.com.cn/record/111777441
开 O2:https://www.luogu.com.cn/record/111777469
可以看到有个点不开 O2 跑得飞快,开 O2 T 飞了。
我把 gcd 换成了我自己的。结果发现最后一个点不开 O2 只要 82ms。开 O2 也 T 飞了。
想问一下什么原因导致的。