P4718 【模板】Pollard-Rho算法

不知道为什么原来的板子一直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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值