Pollard-Rho(luguP4718)

题目链接:https://www.luogu.org/problemnew/show/P4718

思路:Pollard-Rho模板,这题卡常了,Pollard-Rho要倍增写

#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <bitset>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef vector<long long> VI;
typedef unsigned long long ull;
const ll inff = 0x3f3f3f3f3f3f3f3f;
#define FOR(i,a,b) for(int i(a);i<=(b);++i)
#define FOL(i,a,b) for(int i(a);i>=(b);--i)
#define SZ(x) ((long long)(x).size())
#define REW(a,b) memset(a,b,sizeof(a))
#define inf int(0x3f3f3f3f)
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define ss(a) scanf("%s",a)
#define mod ll(1e9+7)
#define pb push_back
#define eps 1e-6
#define lc d<<1
#define rc d<<1|1
#define Pll pair<ll,ll>
#define P pair<int,int>
#define pi acos(-1)
ll pr;
ll pmod(ll a,ll b,ll p){return (a*b-(ll)((long double)a/p*b)*p+p)%p;}//普通的快速乘会T
ll gmod(ll a,ll b,ll p)
{
    ll res=1;
    while(b)
    {
        if(b&1) res=pmod(res,a,p);
        a=pmod(a,a,p);b>>=1;
    }
    return res;
}
bool Miller_Rabin(ll n)
{
    if(n==2) return 1;
    if(n<2||!(n&1)) return 0;
    ll m=n-1,k=0;
    while (!(m&1)) k++,m>>=1;
    FOR(i,1,20)  // 20为Miller-Rabin测试的迭代次数
    {
        ll a=rand()%(n-1)+1,x=gmod(a, m, n),y;
        FOR(j,1,k)
        {
            y=pmod(x,x,n);
            if (y==1&&x!=1&&x!=n-1) return 0;
            x=y;
        }
        if(y!=1) return 0;
    }
    return 1;
}
ll Pollard_Rho(ll x)
{
    ll n=0,m=0,t=1,q=1,c=rand()%(x-1)+1;
    for(ll k=2;;k<<=1,m=n,q=1)
    {
    	for(ll i=1;i<=k;++i)
    	{
    		n=(pmod(n,n,x)+c)%x;
    		q=pmod(q,abs(m-n),x);
        }
        t=__gcd(x,q);if(t>1)return t;
    }
}
void fid(ll n)
{
	if(n==1) return;
	if(Miller_Rabin(n)) {pr=max(pr,n);return;}
	ll p=n;
	while(p>=n) p=Pollard_Rho(p);
	fid(p);fid(n/p);
}
int main()
{
    cin.tie(0);
    cout.tie(0);
    ll zz,n;
    cin>>zz;
    while(zz--)
    {
        sl(n);pr=0;
        fid(n);
        if(pr==n) puts("Prime");
        else printf("%lld\n",pr);
    }
    return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值