特别声明
- 本人的代码被这篇的数据给搞挂了,下面采用的因子库个数为 12。
- 原本采用的因子个数为 5,所以可能会对博客中给出的数据造成一定的干扰,但大致走向是不变的。
- 更新后的代码可以 AC该模板题。
- 再下面还有随机化的版本,可以适用原来博客中的数据。
#include<fstream>
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<cmath>
#define auto unsigned long long
using namespace std;
const int tl=12;
const auto p[]={2,3,5,7,11,13,17,19,23,29,31,37};
auto product(auto x,auto y,auto mod)
{
return (__int128)x*y%mod;
}
auto gcd(auto a,auto b)
{
return (b==0)?a:gcd(b,a%b);
}/*
auto gcd(auto a,auto b)//快速?gcd
{
if(!a||!b)return a|b;//这个特判是有用的
int k=__builtin_ctzll(a|b);//作用是取某个数最后一位0的个数
b>>=__builtin_ctzll(b);
while(a)
{
a>>=__builtin_ctzll(a);
if(a<b)swap(a,b);
a-=b;
}
return (b<<k);
}*/
auto power(auto x,auto a,auto mod)//快速幂的另一种写法
{
auto res;
for(res=1;a!=0;a>>=1)
{
if((a&1)!=0)res=product(res,x,mod);
x=product(x,x,mod);
}
return res;
}
bool Miller_Rabin(auto x)
{
if(x==1)return 0;//特判
auto v1=x-1;
while((v1&1)==0)v1>>=1;
for(int i=0;i<tl;i++)
{
if(x==p[i])return 1;//显然
bool ok=0;
auto v=v1;
auto y=power(p[i],v,x);
if(y==1)ok=1;
else
{
while(v<x-1)
{
if(y==x-1)
{
ok=1;
break;
}
v<<=1;
y=product(y,y,x);
}
}
if(!ok)return 0;
}
return 1;
}
auto big_rand()//[0,2^45)
{
auto x=rand();
for(register int i=1;i<3;i++)
x=(x<<15|rand());
return rand();
}
auto seed,step;
auto f(auto a,auto mod)
{
return (product(a,a,mod)+seed)%mod;
}
auto floyd(auto n) //绕环
{
seed=big_rand()%n;
// cout<<seed;
auto fast,slow,res=1;
fast=slow=big_rand()%n;
fast=f(fast,n);//注意一开始不要先跳两步
for(register int i=0;fast!=slow;i++)
{
res=product(res,fast-slow+n,n);//段取优化
if(i%step==0)
{
auto g=gcd(res,n);
if(g!=1)return g;
}
fast=f(f(fast,n),n);
slow=f(slow,n);
}
// for(;;);
return gcd(res,n);
}
auto mx;
void Pollard_rho(auto n)
{
if(n==1)return;//特判1
// cout<<n<<endl;
if(Miller_Rabin(n))
{
mx=max(mx,n);
return;
}
auto k=1;
step=log(n);
step=(step<<1)+1; //段取优化
while(k==1)k=floyd(n);
// cout<<n<<'!'<<k<<endl;
Pollard_rho(k);
while(n%k==0)n/=k;
Pollard_rho(n);
}
int main()
{
srand(time(NULL));
int T;auto n;
scanf("%d",&T);
while(T--)
{
mx=0;
scanf("%lld",&n);
Pollard_rho(n);
if(mx!=n)printf("%lld\n",mx);
else printf("Prime\n");
}
return 0;
} #include<fstream>
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<cmath>
#define auto unsigned long long
using namespace std;
auto product(auto x,auto y,auto mod)
{
return (__int128)x*y%mod;
}
auto gcd(auto a,auto b)
{
return (b==0)?a:gcd(b,a%b);
}/*
auto gcd(auto a,auto b)//快速?gcd
{
if(!a||!b)return a|b;//这个特判是有用的
int k=__builtin_ctzll(a|b);//作用是取某个数最后一位0的个数
b>>=__builtin_ctzll(b);
while(a)
{
a>>=__builtin_ctzll(a);
if(a<b)swap(a,b);
a-=b;
}
return (b<<k);
}*/
auto power(auto x,auto a,auto mod)//快速幂的另一种写法
{
auto res;
for(res=1;a!=0;a>>=1)
{
if((a&1)!=0)res=product(res,x,mod);
x=product(x,x,mod);
}
return res;
}
auto Miller_Rabin(auto x)
{
if(x==1)return 0;//特判
if(x<=3)return 1;//再特判
for(int i=0;i<5;i++)
{
bool ok=0;
auto v=x-1,p=rand()%(x-3)+2;
while((v&1)==0)v>>=1;
auto y=power(p,v,x);
if(y==1)ok=1;
else
{
while(v<x-1)
{
if(y==x-1)
{
ok=1;
break;
}
v<<=1;
y=product(y,y,x);
}
}
if(!ok)return 0;
}
return 1;
}
auto seed,step;
auto f(auto a,auto mod)
{
return (product(a,a,mod)+seed)%mod;
}
auto floyd(auto n) //绕环
{
seed=n-rand()%n;
// cout<<seed;
auto fast,slow,res=1;
fast=slow=n-rand()%n;
fast=f(fast,n);//注意一开始不要先跳两步
for(register int i=0;fast!=slow;i++)
{
res=product(res,fast-slow+n,n);//段取优化
if(i%step==0)
{
auto g=gcd(res,n);
if(g!=1)return g;
}
fast=f(f(fast,n),n);
slow=f(slow,n);
}
// for(;;);
return gcd(res,n);
}
auto mx;
void Pollard_rho(auto n)
{
if(n==1)return;//特判1
// cout<<n<<endl;
auto x=Miller_Rabin(n);
if(x==1)
{
mx=max(mx,n);
return;
}
auto k=1;
if(x!=0)k=x;
step=log(n);
step=(step<<1)+1; //段取优化
while(k==1)k=floyd(n);
// cout<<n<<'!'<<k<<endl;
Pollard_rho(k);
while(n%k==0)n/=k;
Pollard_rho(n);
}
int main()
{
// freopen("STD","r",stdin);
// freopen("STD1","w",stdout);
srand(time(NULL));
int T;auto n;
scanf("%d",&T);
while(T--)
{
mx=0;
scanf("%lld",&n);
Pollard_rho(n);
if(mx!=n)printf("%lld\n",mx);
else printf("Prime\n");
}
return 0;
} - 如果你真的愿意看到这里,那么这里还有一个在细节上优化的版本,快了很多:
#include<fstream>
#include<cmath>
#define auto long long
using namespace std;
const int tl=12;
const auto p[]={2,3,5,7,11,13,17,19,23,29,31,37};
inline auto product(auto x,auto y,auto n)//防爆乘
{
register long long ans=
x*y-(long long)((long double)x*y/n+0.5)*n;//技巧性极强
return ans<0?ans+n:ans;
}
auto gcd(auto a,auto b)
{
return (b==0)?a:gcd(b,a%b);
}
inline auto power(auto x,auto a,auto mod)//快速幂的另一种写法
{
auto res;
for(res=1;a!=0;a>>=1)
{
if((a&1)!=0)res=product(res,x,mod);
x=product(x,x,mod);
}
return res;
}
inline bool Miller_Rabin(auto x)
{
if(x<=2)return x==2;//特判
auto v1=x-1;
while((v1&1)==0)v1>>=1;
for(int i=0;i<tl;i++)
{
if(x==p[i])return 1;//显然
bool ok=0;
auto v=v1;
auto y=power(p[i],v,x);
if(y==1)ok=1;
else
{
while(v<x-1)
{
if(y==x-1)
{
ok=1;
break;
}
v<<=1;
y=product(y,y,x);
}
}
if(!ok)return 0;
}
return 1;
}
inline auto big_rand()
{
static auto sd=19260817;
sd^=sd<<13;
sd^=sd>>17;
return abs(sd^=sd<<5);
}
auto seed,step;
inline auto f(auto a,auto mod)
{
return (product(a,a,mod)+seed)%mod;
}
inline auto floyd(auto n) //绕环
{
seed=big_rand()%n;
auto fast,slow,res=1;
fast=slow=big_rand()%n;
fast=f(fast,n);//注意一开始不要先跳两步
for(register int i=0;fast!=slow;i++)
{
res=product(res,fast-slow+n,n);//段取优化
if(!res)res=fast-slow+n;
if(i%step==0)
{
auto g=gcd(res,n);
if(g!=1)return g;
res=1;
}
fast=f(f(fast,n),n);
slow=f(slow,n);
}
return gcd(res,n);
}
auto mx;
void Pollard_rho(auto n)
{
if(n==1)return;
if(Miller_Rabin(n))
{
mx=max(mx,n);
return;
}
auto k=1;
step=log(n);
step=step<<1|1;
while(k==1)k=floyd(n);
Pollard_rho(k);
Pollard_rho(n/k);
}
int main()
{
int T;auto n;
scanf("%d",&T);
while(T--)
{
mx=0;
scanf("%lld",&n);
Pollard_rho(n);
if(mx!=n)printf("%lld\n",mx);
else printf("Prime\n");
}
return 0;
}