版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sizeof_you/article/details/85643692
就是个板子···
然而
上最后一个点太毒了
分弃疗
复杂度是
的,
是
的某个最小因子,满足
与
互质
对于复杂度的优化可以用真正地快速乘,以及 二进制版 ,还有在中间每次 据说可以更快,然而这还是不能过掉最后一个点,看过掉的人都是用确定 个质数那样做 ,但那样好像正确性不能保证??
分代码如下:
#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int mod=10000019;
const int tim=5;
const int M=(1<<7)-1;
LL mx,n;
inline LL mul(LL x,LL y,LL MOD){
LL tmp=(x*y-(LL)((long double)x/MOD*y+1e-8)*MOD);
return tmp<0?tmp+MOD:tmp;
}
inline LL qpow(LL a,LL b,LL m){
LL ret=1; a%=m;
while(b){
if(b&1) ret=mul(a,ret,m);
b>>=1; a=mul(a,a,m);
} ret%=m; return ret;
}
inline bool Miller_Rabin(LL n,int t){
if(n==2 || n==3 || n==5) return true;
if(n%2==0 || n==1 || n==46856248255981ll) return false;
LL d=n-1; int s=0;
while(!(d&1)) ++s,d>>=1;
for(int i=0;i<t;i++){
LL a=rand()%(n-3)+2;
LL x=qpow(a,d,n),y=0;
for(int j=0;j<s;j++){
y=mul(x,x,n);
if(y==1 && x!=1 && x!=n-1) return false;
x=y;
}
if(y!=1) return false;
} return true;//
}
//LL gcd(LL a,LL b){return b==0?a:gcd(b,a%b);}
LL gcd(LL a, LL b) {
if (!a) return b;
if (!b) return a;
#define ctz __builtin_ctzll
int t = ctz(a | b);
a >>= ctz(a);
do {
b >>= ctz(b);
if (a > b) {
LL tmp = b;
b = a;
a = tmp;
}
b -= a;
} while (b != 0);
return a << t;
}//???
LL Pollard_Rho(LL n,LL c){
if(n%2==0) return 2;
if(n%3==0) return 3;
if(n%5==0) return 5;
if(n%7==0) return 7;
if(n%61==0) return 61;
if(n%24251==0) return 24251;
LL x=rand()%(n-2)+1,y=x,i=1,k=2,d;
while(1){
++i;
x=(mul(x,x,n)+c)%n;
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 || n<=mx) return;
if(Miller_Rabin(n,tim)) {mx=max(mx,n);return;}
LL p=n;
while(p>=n) p=Pollard_Rho(p,c--);
find(p,c); find(n/p,c);
}
int T;
int main(){
srand((unsigned)time(NULL));
scanf("%d",&T);
while(T--){
scanf("%lld",&n); mx=0;
find(n,rand()%(n-1)+1);
if(mx==n) puts("Prime");
else printf("%lld\n",mx);
}
return 0;
}