P4718 【模板】Pollard-Rho算法
题意:
给定整数n,判断是否是质数,是质数就输出Prime;
如果不是质数,输出它的最大质因子。
数据范围:n<=1e18
code:
用了__int128,这个代码只能求最大质因子,不能用来求质因子的幂次。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define lll __int128
int t;
ll max_factor, n;
ll gcd(ll a, ll b) {
if (b == 0) return a;
return gcd(b, a % b);
}
ll quick_pow(ll x, ll p, ll mod) {
ll ans = 1;
while (p) {
if (p & 1) ans = (lll)ans * x % mod;
x = (lll)x * x % mod;
p >>= 1;
}
return ans;
}
bool Miller_Rabin(ll p) {
if (p < 2) return 0;
if (p == 2) return 1;
if (p == 3) return 1;
ll d = p - 1, r = 0;
while (!(d & 1)) ++r, d >>= 1;
for (ll k = 0; k < 10; ++k) {
ll a = rand() % (p - 2) + 2;
ll x = quick_pow(a, d, p);
if (x == 1 || x == p - 1) continue;
for (int i = 0; i < r - 1; ++i) {
x = (lll)x * x % p;
if (x == p - 1) break;
}
if (x != p - 1) return 0;
}
return 1;
}
ll f(ll x, ll c, ll n) { return ((lll)x * x + c) % n; }
ll Pollard_Rho(ll x) {
ll s = 0, t = 0;
ll c = (ll)rand() % (x - 1) + 1;
int step = 0, goal = 1;
ll val = 1;
for (goal = 1;; goal <<= 1, s = t, val = 1) {
for (step = 1; step <= goal; ++step) {
t = f(t, c, x);
val = (lll)val * abs(t - s) % x;
if ((step % 127) == 0) {
ll d = gcd(val, x);
if (d > 1) return d;
}
}
ll d = gcd(val, x);
if (d > 1) return d;
}
}
void fac(ll x) {
if (x <= max_factor || x < 2) return;
if (Miller_Rabin(x)) {
max_factor = max(max_factor, x);
return;
}
ll p = x;
while (p >= x) p = Pollard_Rho(x);
while ((x % p) == 0) x /= p;
fac(x), fac(p);
}
int main() {
scanf("%d", &t);
while (t--) {
srand((unsigned)time(NULL));
max_factor = 0;
scanf("%lld", &n);
fac(n);
if (max_factor == n)
printf("Prime\n");
else
printf("%lld\n", max_factor);
}
return 0;
}
质因子分解(可以计算质因子指数):
code:
没有用__int128,防爆longlong用的是快速乘
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxm=2e5+5;
ll temp[maxm],cnt;//存所有质因子
//板子:
ll gcd(ll a, ll b) {
return b ? gcd(b, a % b) : a;
}
ll quick_mult(ll a, ll b, ll mod) {
ll ans = 0;
while(b) {
if(b & 1) ans = (ans + a) % mod;
a = (a + a) % mod;
b >>= 1;
}
return ans;
}
ll quick_pow(ll a, ll n, ll mod) {
ll ans = 1;
while(n) {
if(n & 1) ans = quick_mult(ans, a, mod);
a = quick_mult(a, a, mod);
n >>= 1;
}
return ans;
}
bool miller_rabin(ll n) {
if(n == 2) return true;
if(n < 2 || !(n & 1)) return false;
ll s = 0, d = n - 1;
while(!(d & 1)) {
d >>= 1;
s++;
}
for(int i = 1; i <= 11; i++) {
ll a = rand() % (n - 2) + 2;
ll now = quick_pow(a, d, n), pre = now;
for(int j = 1; j <= s; j++) {
now = quick_mult(now, now, n);
if(now == 1 && pre != 1 && pre != n - 1) return false;
pre = now;
}
if(now != 1) return false;
}
return true;
}
ll pollard_rho(ll n, int c) {
ll x, y, i = 1, k = 2;
x = y = rand() % (n - 2) + 2;
for( ; ; ) {
i++;
x = (quick_mult(x, x, n) + c) % n;
ll g = gcd(y - x, n);
if(g > 1 && g < n) return g;
if(x == y) return n;
if(i == k) y = x, k <<= 1;
}
}
void find_fac(ll n, int k) {
if(n == 1) return ;
if(miller_rabin(n)) {
temp[++cnt]=n;
return ;
}
ll p = n;
int c = k;
while(p >= n) p = pollard_rho(p, c--);
find_fac(p, k);
find_fac(n / p, k);
}
//
int main(){
return 0;
}