# Miller-Rabin & Pollard-rho

$a^{n-1} \equiv 1 \pmod n$

$x^2 \equiv 1 \pmod p^e$

Pollard-rho启发式因子分解期望$O(\sqrt{p})$找到一个为p的质因子

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
char c=getchar();ll x=0,f=1;
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return x*f;
}
ll n;
ll Mul(ll a, ll b, ll P) {
ll t = (a*b - (ll)((long double)a/P*b+1e-8)*P);
return t<0 ? t+P : t;
}
ll Pow(ll a, ll b, ll P) {
ll ans=1; a%=P;
for(; b; b>>=1, a=Mul(a, a, P))
if(b&1) ans=Mul(ans, a, P);
return ans;
}
bool witness(ll a, ll n, ll u, int t) {
ll x=Pow(a, u, n), y=x;
for(int i=1; i<=t; i++) {
x=Mul(x, x, n);
if(x==1 && y!=1 && y!=n-1) return true;
y=x;
}
return x!=1;
}
bool MillerRabin(ll n) {
if(n==2) return true;
if(n<=1 || !(n&1)) return false;
ll u=n-1, t=0;
while(!(u&1)) u>>=1, t++;
for(int i=1; i<=10; i++)
if(witness(rand()%(n-1)+1, n, u, t)) return false;
return true;
}
ll gcd(ll a, ll b) {return b==0?a:gcd(b, a%b);}
ll rho(ll n, ll c) {
int k=2; ll x=rand()%n, y=x, d=1;
for(int i=1; d==1; i++) {
x=(Mul(x,x,n)+c)%n;
d=gcd(n, y>x?y-x:x-y);
if(i==k) y=x, k<<=1;
}
return d;
}
ll Max;
void solve(ll n) {
if(n==1) return;
if(MillerRabin(n)) {Max=max(Max, n); return;}
ll t=n;
while(t==n) t=rho(n, rand()%(n-1)+1);
solve(t); solve(n/t);
}

int main() {
freopen("in","r",stdin);
srand(317);