https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1162
数据范围大约是2^97,需要高精度计算
可以使用pollard-rho算法,期望需要$O((logn)^{1/4})$次整数操作
需要采用 蒙哥马利约化(Montgomery reduction)避免大量取模,并每隔一段时间检查一次gcd。
#include<bits/stdc++.h>
typedef __int128 num;
typedef long long i64;
const int B=,K=;
bool np[B+];
int ps[B],pp=,pps[B],fp=,qp=;
num fs[],P,qs[];
num read(){
num x=;
int c=getchar();
while(!isdigit(c))c=getchar();
while(isdigit(c))x=x*+c-,c=getchar();
return x;
}
void pr(num x){
if(x<)putchar('-'),x=-x;
int ss[],sp=;
do ss[++sp]=x%;while(x/=);
while(sp)putchar(+ss[sp--]);
putchar();
}
num rnd(){
static std::mt19937 mt(time());
num z=mt();
z=z<<^mt();
z=z<<^mt();
return z%(P-)+;
}
const unsigned X=(<<)-;
inline num fix1(num a){return a>=P?a-P:a;}
inline num fix2(num a){return a<?a+P:a;}
inline num mm(num a,num b,num C=){
unsigned b0=b&X;b>>=;
unsigned b1=b&X;b>>=;
unsigned b2=b&X;b>>=;
unsigned b3=b&X;
num c=a*b3%P;
c=((c<<)+a*b2)%P;
c=((c<<)+a*b1)%P;
c=((c<<)+a*b0+C)%P;
return c;
}
num iP,RR,iR;
#define HI(x) u64((x)>>52)
#define LO(x) u64(x&((1ll<<52)-1))
typedef unsigned __int128 u128;
typedef unsigned long long u64;
const num R=num()<<,R1=R-;
__attribute__((optimize("Ofast")))
inline u128 mul(u128 a,u128 b){
u128 m=a*b*iP&R1;
u128 t=(
(u128)HI(a)*HI(b)+
(u128)HI(m)*HI(P)
);
u128 t1=(
(u128)HI(a)*LO(b)+
(u128)LO(a)*HI(b)+
(u128)HI(m)*LO(P)+
(u128)LO(m)*HI(P)
);
u128 t0=(
(u128)LO(a)*LO(b)+
(u128)LO(m)*LO(P)
);
t+=HI(HI(t0)+LO(t1))+HI(t1);
return fix1(t);
}
inline u128 mulRR(u128 a){return mul(a,RR);}
num exgcd(num a,num b,num&x,num&y){
if(!b){x=,y=;return a;}
num g=exgcd(b,a%b,y,x);
y-=a/b*x;
return g;
}
void setP(num x){
P=x;
exgcd(P,R,iP,iR);
iP=R1&-iP;
iR=fix2(iR);
RR=mm(R%P,R%P);
}
num pw(num a,num n){
num v=;
for(;n;n>>=,a=mm(a,a))if(n&)v=mm(v,a);
return v;
}
bool mr(){
num s=P-;
int r=;
for(;~s&;s>>=,++r);
num a=pw(rnd(),s);
for(;;){
if(a==||a==P-)return ;
if(!--r)return ;
a=mm(a,a);
}
}
bool isp(){
if(P<=B)return !np[P];
if(~P&)return ;
for(int i=;i<;++i)if(!mr())return ;
return ;
}
num gcd(num a,num b){return b?gcd(b,a%b):a;}
num abs(num x){return x>?x:-x;}
void pollard_rho(num n){
setP(n);
if(isp()){
fs[fp++]=n;
return;
}
const int mod=;
for(;;){
num a,b,c,prod=mulRR();
a=b=mulRR(rnd());
c=mulRR(rnd());
for(i64 ii=,k=;;){
a=fix1(mul(a,a)+c);
prod=mul(prod,abs(a-b));
if(!prod){
prod=abs(a-b);
num g=gcd(P,mul(prod,));
if(g==n)break;
if(g!=){
qs[qp++]=g;
qs[qp++]=n/g;
return;
}
}
if(++ii==k)k<<=,b=a;
if(!(ii&mod)){
num g=gcd(P,mul(prod,));
if(g==n)break;
if(g!=){
qs[qp++]=g;
qs[qp++]=n/g;
return;
}
}
}
}
}
void factor(num x){
for(int i=;i<pp;++i){
for(int p=ps[i];x%p==;fs[fp++]=p,x/=p);
}
if(x>)qs[qp++]=x;
while(qp)pollard_rho(qs[--qp]);
std::sort(fs,fs+fp);
for(int i=;i<fp;++i)pr(fs[i]);
}
void init(){
for(int i=;i<=B;++i){
if(!np[i])ps[pp++]=i;
for(int j=;j<pp&&i*ps[j]<=B;++j){
np[i*ps[j]]=;
if(i%ps[j]==)break;
}
}
for(int i=;i<pp;++i){
int p=ps[i],s=;
while(s<=B/p)s*=p;
pps[i]=s;
}
}
int main(){
init();
factor(read());
return ;
}