1. 程式人生 > >[學習筆記] 素數測試與質因數分解 - 學習筆記

[學習筆記] 素數測試與質因數分解 - 學習筆記

copy了luogu上目前rk1的板子
分解質因數其中有一段還不知道在幹嘛:
列舉k是大於等於2的2的冪,每次初始化 x 0 = x c u

r , q = 1 x_0=x_{cur},q=1 ,然後 f
o r   i [ 1 , k ]
   x = F ( x ) , q = q × x x 0 , i f    i > M   a n d   gcd ( q , n ) > 1    b r e a k \mathrm{for\ i\in[1,k]\ \ }x=F(x),q=q\times|x-x_0|,\mathrm{if}\ \ i>M\mathrm{\ and\ }\gcd(q,n)>1\ \ \mathrm{break}
。然後每做完一個k就檢查是否 t > 1   o r   gcd ( q , n ) > 1 t>1\mathrm{\ or\ }\gcd(q,n)>1 。其中 M M 2 7 1 2^7-1

// luogu-judger-enable-o2
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define Rep(i,v) rep(i,0,(int)v.size()-1)
#define lint long long
#define ull unsigned lint
#define db long double
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define gc getchar()
#define debug(x) cerr<<#x<<"="<<x
#define sp <<" "
#define ln <<endl
using namespace std;
typedef pair<int,int> pii;
typedef set<int>::iterator sit;
inline lint inn()
{
    lint x,ch;while((ch=gc)<'0'||ch>'9');
    x=ch^'0';while((ch=gc)>='0'&&ch<='9')
        x=(x<<1)+(x<<3)+(ch^'0');return x;
}
namespace RHO_space{
    const int BAS_cnt=6;int Bas[BAS_cnt]={2,3,7,23,61,24251};
    inline lint tms(lint a,lint b,lint m) { return (lint)((__int128)a*b%m); }
    inline lint gcd(lint a,lint b) { return a?gcd(b%a,a):b; }
    inline lint fast_pow(lint x,lint k,lint m,lint ans=1)
    {	for(;k;k>>=1,x=tms(x,x,m)) (k&1)?ans=tms(ans,x,m):0;return ans; }
    inline bool Miller(lint x)
    {
        //if(x==46856248255981ll||x<2) return false;//???
        if(x<2) return false;
        rep(i,0,BAS_cnt-1) if(x%Bas[i]==0) return x==Bas[i];
        lint b=x-1;int t=0;while(!(b&1)) b>>=1,t++;
        rep(i,0,BAS_cnt-1)
        {
            if(Bas[i]>x) break;
            lint cur=fast_pow(Bas[i],b,x),las=cur;
            rep(j,1,t)
            {
                cur=tms(cur,cur,x);
                if(cur==1&&las!=1&&las!=x-1) return false;
                las=cur;
            }
            if(cur!=1) return false;
        }
        return true;
    }
    inline lint gabs(lint x) { return x<0?-x:x; }
    inline lint G(lint x,lint n,lint a) { lint t=tms(x,x,n)+a;return t>=n?t-n:t; }
    const int M=(1<<7)-1;
    struct RAND{
        ull x;RAND() { x=0; }
        inline ull operator()() { return ((x+=998244353)*=1000000007)^=19260817; }
    }rnd;
    inline lint Rho(lint n)
    {
        if(n%2==0) return 2;if(n%3==0) return 3;if(n%5==0) return 5;
        lint x=0,y=0,t=1,q=1,c=rnd()%(n-1)+1;
        for(int k=2;;k<<=1,y=x,q=1)
        {
            rep(i,1,k)
            {
                x=G(x,n,c),q=tms(q,gabs(x-y),n);
                if(!(i&M)) { if((t=gcd(q,n))>1) break; }
            }
            if(t>1||(t=gcd(q,n))>1) break;
        }
        return t;
    }
    lint mxp;
    inline int Find(lint x)
    {
        if(x<=mxp||x==1) return 0;if(Miller(x)) return mxp=max(mxp,x),0;
        lint t=x;while(t==x) t=Rho(t);while(x%t==0) x/=t;return Find(x),Find(t);
    }
    inline lint solve(lint x) { return mxp=0,Find(x),mxp; }
}using RHO_space::solve;
int main()
{
    for(lint T=inn();T;T--)
    {
        lint n=inn(),ans=solve(n);
        if(ans==n) printf("Prime\n");
        else printf("%lld\n",ans);
    }
    return 0;
}