Test instructions: Given an n (2 <= N < 2^54), if it is prime, the output "prime", otherwise the smallest element factor
Worship Bin Giant
#include <iostream>#include<cstdio>#include<cstring>#include<string>#include<queue>#include<cmath>#include<stdlib.h>#include<time.h>#include<algorithm>using namespacestd;/************************************************** * Miller_rabin algorithm for prime test * fast, you can judge a < 2^63 number is not prime * ********* *****************************************/Const ints=8;//number of random algorithm judgments//calculate ret= (a*b)%c a,b,c<2^63Long LongMult_mod (Long LongALong LongBLong Longc) {a%=C; b%=C; Long Longret=0; Long Longtmp=A; while(b) {if(b&1) {ret+=tmp; if(ret>c) Ret-=c;//direct modulo is much slower} tmp<<=1; if(tmp>c) tmp-=C; b>>=1; } returnret;}//calculate ret= (a^n)%modLong LongPow_mod (Long LongALong LongNLong LongMoD) { Long Longret=1; Long Longtemp=a%MoD; while(n) {if(n&1) ret=Mult_mod (RET,TEMP,MOD); Temp=Mult_mod (TEMP,TEMP,MOD); N>>=1; } returnret;}//using a^ (n-1) =1 (mod n) to determine if n is a prime number//n-1=x*2^t Two judgments in the middle of the application//is composite returns true, not necessarily composite returns falseBOOLCheckLong LongALong LongNLong LongXLong Longt) { Long Longret=Pow_mod (a,x,n); Long Longlast=ret; for(intI=1; i<=t;i++) {ret=Mult_mod (ret,ret,n); if(ret==1&&last!=1&&last!=n-1)return true;//Compositelast=ret; } if(ret!=1)return true; return false;}//**************************************************//Miller_rabin Algorithm//is a prime number that returns True, (possibly pseudo prime)//not a prime return false//**************************************************BOOLMiller_rabin (Long LongN) { if(n<2)return false; if(n==2)return true; if((n&1)==0)return false;//even Long Longx=n-1; Long Longt=0; while((x&1)==0) {x>>=1; T++; } Srand (Time (NULL)); for(intI=0; i<s;i++) { Long LongA=rand ()% (n1)+1; if(check (a,n,x,t))return false; } return true;}//**********************************************//Pollard_rho algorithm for quality factor decomposition//*********************************************Long Longfactor[ -];//mass factor decomposition results (just returning from the disorder)intTol//Number of qualitative factors, ref. 0~TOL-1Long LonggcdLong LongALong Longb) { Long LongT; while(b) {T=A; A=b; b=t%b; } if(a>=0)returnA; return-A;}//Find a factorLong LongPollard_rho (Long LongXLong Longc) { Long LongI=1, k=2; Srand (Time (NULL)); Long LongX0=rand ()% (x1)+1; Long Longy=x0; while(1) {i++; X0= (Mult_mod (x0,x0,x) +c)%x; Long LongD=GCD (yx0,x); if(d!=1&&D!=X)returnD; if(y==x0)returnx; if(i==k) {y=x0; K+=K; } }} //The n factor decomposition, deposited factor. K set to about 107voidFINDFAC (Long LongNintk) { if(n==1)return; if(Miller_rabin (n)) {Factor[tol++]=N; return; } Long Longp=N; intC=K; while(p>=n) P=pollard_rho (p,c--);//value change, prevent dead loop KFINDFAC (p,k); FINDFAC (n/p,k);}//POJ 1811//given an n (2 <= N < 2^54), if it is a prime number, the output "prime", otherwise the least element factorintMain () {intT; Long LongN; scanf ("%d",&T); while(t--) {scanf ("%i64d",&N); if(Miller_rabin (n)) printf ("prime\n"); Else{tol=0; FINDFAC (N,107); Long Longans=factor[0]; for(intI=1; i<tol;i++) ans=min (ans,factor[i]); printf ("%i64d\n", ans); } } return 0;}
POJ1811 Prime Test (judging random primes)