Problem Description
Given a big integer number, you are required to find out whether it's a prime number.
Input
The first line contains the number of test cases T (1 <= T <= 20 ), then the following T lines each contains an integer number N (2 <= N < 2<sup>54</sup>).
Output
For each test case, if N is a prime number, output a line containing the word "Prime", otherwise, output a line containing the smallest prime factor of N.
Sample Input
2 5 10
Sample Output
Prime 2
//修改一下模板算法即可,在下面的find函数里稍加改变
#include<iostream>
#include<cstdio>
#include<ctime>
#include<cmath>
#include<cstring>
#include<cstdlib>
#define C 240
#define N 5500
#define times 10
#define LL long long
using namespace std;
LL ct,cnt;
LL fac[N],num[N];
LL ans;
LL gcd(LL a,LL b)
{
return b?gcd(b,a%b):a;
}//最小公约数
LL multi(LL a,LL b,LL m)
{
LL ans=0;
a%=m;
while(b){
if(b&1)
{
ans=(ans+a)%m;
b--;
}
b>>=1;
a=(a+a)%m;
}
return ans;
}
LL quick_mod(LL a,LL b,LL m)
{
LL ans=1;
a%=m;
while(b)
{
if(b&1)
{
ans=multi(ans,a,m);
b--;
}
b>>=1;
a=multi(a,a,m);
}
return ans;
}
bool Miller_Rabin(LL n)
{
if(n==2)return true;
if(n<2||!(n&1)) return false;
LL m=n-1;
int k=0;
while((m&1)==0)
{
k++;
m>>=1;
}
for(int i=0;i<times;i++)
{
LL a=rand()%(n-1)+1;
LL x=quick_mod(a,m,n);
LL y=0;
for(int j=0;j<k;j++)
{
y=multi(x,x,n);
if(y==1&&x!=1&&x!=n-1)return false;
x=y;
}
if(y!=1)return false;
}
return true;
}
LL pollard_rho(LL n,LL c)
{
LL i=1,k=2;
LL x=rand()%(n-1)+1;
LL y=x;
while(true)
{
i++;
x=(multi(x,x,n)+c)%n;
LL d=gcd((y-x+n)%n,n);
if(1<d&&d<n)return d;
if(y==x)return n;
if(i==k)
{
y=x;
k<<=1;
}
}
}
void find(LL n,LL c)
{
if(n==1)return;
if(Miller_Rabin(n))
{
ans=min(ans,n);
return;
}
LL p=n;
LL k=c;
while(p>=n)p=pollard_rho(p,c--);
find(p,k);
find(n/p,k);
}
int main()
{
LL n;
int t;
scanf("%d",&t);
while(t--)
{
cin>>n;
if(Miller_Rabin(n))
printf("Prime\n");
else
{
ans=n;
find(n,120);
cout<<ans<<endl;
}
}
return 0;
}