【bzoj3667】Rabin-Miller算法 素数测试

AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=3667

【吐槽】

这是一道模板题,但是出了很玄学的错误,交到bzoj上一直wa。

然后要到了数据,用cena评测,然后发现并没有错误。。。

哪位大神知道这种玄学错误的话,欢迎指正,感激不尽。

/*************
  bzoj 3667
  by chty
  2016.11.7
*************/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll prime[20]={2,3,5,7,11,13,17,19,23};
ll T,maxx;
inline ll read()
{
    ll x=0,f=1;  char ch=getchar();
    while(!isdigit(ch))  {if(ch=='-')  f=-1;  ch=getchar();}
    while(isdigit(ch))  {x=x*10+ch-'0';  ch=getchar();}
    return x*f;
}
ll gcd(ll a,ll b) {return !b?a:gcd(b,a%b);}
ll mul(ll x,ll y,ll mod) {return ((x*y-(ll)(((long double)x*y+0.5)/mod)*mod)%mod+mod)%mod;}//一行快速乘
ll fast(ll a,ll b,ll mod) {ll sum=1;for(;b;b>>=1,a=mul(a,a,mod))if(b&1)sum=mul(sum,a,mod);return sum;}
bool Rabin_Miller(ll p,ll a)
{
    if(p==2)  return 1;
    if((p&1==0)||p==1)  return 0;
    ll d=p-1;
    while(!(d&1))  d>>=1; 
    ll m=fast(a,d,p);
    if(m==1)  return 1;
    for(;d<p&&d>=0;m=mul(m,m,p),d<<=1)  {if(m==p-1)  return 1;}
    return 0;
}
bool isprime(ll x)
{
    for(ll i=0;i<9;i++)
    {
        if(prime[i]==x)  return 1;
        if(!Rabin_Miller(x,prime[i]))  return 0;
    }
    return 1;
}
void Pollord_Rho(ll x)
{
    if(isprime(x))  {maxx=max(maxx,x); return;}
    ll c=3;
    while(1)
    {
        ll x1(1),x2(1),i(1),k(2);
        while(1)
        {
            x1=(mul(x1,x1,x)+c)%x;
            ll d=gcd(abs(x1-x2),x);
            if(d>1&&d<x)
            {
                Pollord_Rho(d);
                Pollord_Rho(x/d);
                return;
            }
            if(x1==x2)  break;
            if(++i==k)  k<<=1,x2=x1;
        }
        c++;
    }
}
void solve(ll n)
{
    if(isprime(n))  {puts("Prime"); return;}
    maxx=0;
    Pollord_Rho(n);
    printf("%lld\n",maxx);
}
int main()
{
    freopen("cin.in","r",stdin);
    freopen("cout.out","w",stdout);
    T=read();
    for(ll i=1;i<=T;i++) {ll n=read();solve(n);}
    return 0;
}


 

 

 
 
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值