不知道为什么原来的板子一直T。
所以换了个板子。
讲真,觉得原来的板子没问题啊啊啊啊。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<ctime>
#define ll long long
using namespace std;
const int s=8;
ll maxx=0;
ll mult_mod(ll a,ll b,ll p)
{
a%=p,b%=p;
ll c=(long double)a*b/p;
ll ans=a*b-c*p;
if(ans<0) ans+=p;
else if(ans>=p) ans-=p;
return ans;
}
ll pow_mod(ll a,ll n,ll p)
{
ll ans=1;
a%=p;
while(n)
{
if(n&1) ans=mult_mod(ans,a,p);
a=mult_mod(a,a,p);
n>>=1;
}
return ans;
}
bool check(ll a,ll n, ll x,ll t)
{
ll ret = pow_mod(a,x,n);
ll last=ret;
for(int i=1;i<=t;i++)
{
ret=mult_mod(ret,ret,n);
if(ret==1&&last!=1&&last!=n-1) return true;
last=ret;
}
if(ret!=1) return true;
else return false;
}
bool Miller_Rabin(ll n)
{
if(n<2) return false;
if(n==2) return true;
if((n&1)==0) return false;
ll x=n-1;
ll t=0;
while((x&1)==0)
{
x>>=1;
t++;
}
srand(time(NULL));
for(int i=0;i<s;i++)
{
ll a=rand()%(n-1)+1;
if(check(a,n,x,t))
return false;
}
return true;
}
ll gcd(ll a,ll b)
{
ll t;
while(b)
{
t=a;
a=b;
b=t%b;
}
if(a>=0) return a;
else return -a;
}
ll Pollard_rho(ll x)
{
ll s=0,t=0,c=rand()%(x-1)+1;
ll step=0,goal=1;
ll val=1;
for(goal=1;;goal<<=1,s=t,val=1)
{
for(step=1;step<=goal;++step)
{
t=(mult_mod(t,t,x)+c)%x;
val=mult_mod(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 findfac(ll n)
{
if(n==1) return ;
if(n<=maxx) return ;
if(Miller_Rabin(n))
{
maxx=max(maxx,n);
return ;
}
ll p=n;
while(p>=n) p=Pollard_rho(n);
while(n%p==0)n/=p;
findfac(n);
findfac(p);
}
int main(void)
{
int t;
cin>>t;
while(t--)
{
ll n;
scanf("%lld",&n);
maxx=0;
findfac(n);
if(maxx==n) printf("Prime\n");
else printf("%lld\n",maxx);
}
return 0;
}
两个差不多,一个的t上来就随机化了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<ctime>
#define ll long long
using namespace std;
const int s=8;
ll maxx=0;
ll mult_mod(ll a,ll b,ll p)
{
a%=p,b%=p;
ll c=(long double)a*b/p;
ll ans=a*b-c*p;
if(ans<0) ans+=p;
else if(ans>=p) ans-=p;
return ans;
}
ll pow_mod(ll a,ll n,ll p)
{
ll ans=1;
a%=p;
while(n)
{
if(n&1) ans=mult_mod(ans,a,p);
a=mult_mod(a,a,p);
n>>=1;
}
return ans;
}
bool check(ll a,ll n, ll x,ll t)
{
ll ret = pow_mod(a,x,n);
ll last=ret;
for(int i=1;i<=t;i++)
{
ret=mult_mod(ret,ret,n);
if(ret==1&&last!=1&&last!=n-1) return true;
last=ret;
}
if(ret!=1) return true;
else return false;
}
bool Miller_Rabin(ll n)
{
if(n<2) return false;
if(n==2) return true;
if((n&1)==0) return false;
ll x=n-1;
ll t=0;
while((x&1)==0)
{
x>>=1;
t++;
}
srand(time(NULL));
for(int i=0;i<s;i++)
{
ll a=rand()%(n-1)+1;
if(check(a,n,x,t))
return false;
}
return true;
}
ll gcd(ll a,ll b)
{
ll t;
while(b)
{
t=a;
a=b;
b=t%b;
}
if(a>=0) return a;
else return -a;
}
ll Pollard_rho(ll x)
{
ll s=0,t=rand(),c=rand()%(x-1)+1;
ll step=0,goal=1;
ll val=1;
for(goal=1;;goal<<=1,s=t,val=1)
{
for(step=1;step<=goal;++step)
{
t=(mult_mod(t,t,x)+c)%x;
val=mult_mod(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 findfac(ll n)
{
if(n==1) return ;
if(n<=maxx) return ;
if(Miller_Rabin(n))
{
maxx=max(maxx,n);
return ;
}
ll p=n;
while(p>=n) p=Pollard_rho(n);
while(n%p==0)n/=p;
findfac(n);
findfac(p);
}
int main(void)
{
int t;
cin>>t;
while(t--)
{
ll n;
scanf("%lld",&n);
maxx=0;
findfac(n);
if(maxx==n) printf("Prime\n");
else printf("%lld\n",maxx);
}
return 0;
}
不剪枝也比原来快啊。。。为啥。。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<ctime>
#define ll long long
using namespace std;
const int s=8;
ll maxx=0;
ll mult_mod(ll a,ll b,ll p)
{
a%=p,b%=p;
ll c=(long double)a*b/p;
ll ans=a*b-c*p;
if(ans<0) ans+=p;
else if(ans>=p) ans-=p;
return ans;
}
ll pow_mod(ll a,ll n,ll p)
{
ll ans=1;
a%=p;
while(n)
{
if(n&1) ans=mult_mod(ans,a,p);
a=mult_mod(a,a,p);
n>>=1;
}
return ans;
}
bool check(ll a,ll n, ll x,ll t)
{
ll ret = pow_mod(a,x,n);
ll last=ret;
for(int i=1;i<=t;i++)
{
ret=mult_mod(ret,ret,n);
if(ret==1&&last!=1&&last!=n-1) return true;
last=ret;
}
if(ret!=1) return true;
else return false;
}
bool Miller_Rabin(ll n)
{
if(n<2) return false;
if(n==2) return true;
if((n&1)==0) return false;
ll x=n-1;
ll t=0;
while((x&1)==0)
{
x>>=1;
t++;
}
srand(time(NULL));
for(int i=0;i<s;i++)
{
ll a=rand()%(n-1)+1;
if(check(a,n,x,t))
return false;
}
return true;
}
ll gcd(ll a,ll b)
{
ll t;
while(b)
{
t=a;
a=b;
b=t%b;
}
if(a>=0) return a;
else return -a;
}
ll Pollard_rho(ll x)
{
ll s=0,t=0,c=rand()%(x-1)+1;
ll step=0,goal=1;
ll val=1;
for(goal=1;;goal<<=1,s=t,val=1)
{
for(step=1;step<=goal;++step)
{
t=(mult_mod(t,t,x)+c)%x;
val=mult_mod(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;
}
}
ll fac[200],tot=0;
void findfac(ll n)
{
if(n==1) return ;
//if(n<=maxx) return ;
if(Miller_Rabin(n))
{
fac[++tot]=n;
return ;
}
ll p=n;
while(p>=n) p=Pollard_rho(n);
while(n%p==0)n/=p;
findfac(n);
findfac(p);
}
int main(void)
{
int t;
cin>>t;
while(t--)
{
ll n;
scanf("%lld",&n);
if(Miller_Rabin(n))
{
printf("Prime\n");
continue;
}
maxx=0;
tot=0;
findfac(n);
for(int i=1;i<=tot;i++)
maxx=max(maxx,fac[i]);
printf("%lld\n",maxx);
}
return 0;
}