Test instructions: Asks if an integer is a prime number, if not, it outputs its smallest mass factor.
Analysis:
Determines whether a large integer is a prime number using the Miller_rabin algorithm, the POLLARD_RHO algorithm is used to find all factorization of a large integer. This problem is the direct set of templates.
In addition the GCD and pow_mod here can not be used in the normal way, T. What I have commented out in the code is the notation of T.
Code:
#include <iostream> #include <cmath> #include <ctime> #include <cstdio> #include <algorithm >using namespace Std;typedef Long long ll;int t;ll n,prim[1000];ll ans;int tot;ll mul_mod (ll a,ll b,ll c) {ll t=0;a%=c;b %=c;while (b) {if (b&1) {t= (t+a)%c;} A<<=1;a%=c;b>>=1;} return t;} ll Pow_mod (ll a,ll b,ll c)//{//ll T=1;//while (b) {//if (b&1) t= (t*a)%c;//b>>=1;//a= (a*a)%c;//}//return t;// }ll Pow_mod (ll x,ll n,ll MoD) {if (n==1) return x%mod; int bit[90],k=0; while (n) {bit[k++]=n&1; n>>=1; } ll Ret=1; for (k=k-1;k>=0;k--) {ret=mul_mod (ret,ret,mod); if (bit[k]==1) ret=mul_mod (RET,X,MOD); } return ret;} BOOL Check (ll a,ll n,ll X,ll t) {ll ret=pow_mod (a,x,n); ll tmp=ret;for (int i=0;i<t;i++) {ret=mul_mod (ret,ret,n); if (ret ==1&&tmp!=1&&tmp!=n-1) return True;tmp=ret;} if (ret!=1) return True;return false;} BOOL Miller_rabin (ll N) {ll x=n-1,t=0;while ((x&1) ==0) {x>>=1;t++;} Int Ok=1;if (t>=1&& (x&1)) {for (int i=0;i<20;i++) {ll a=rand ()% (n-1) +1;if (check (a,n,x,t)) {ok=1;break;} ok=0;}} if (!ok| | n==2) return False;return true;} ll GCD (ll A,ll b)//{//if (b==0) return A;//return gcd (b,a%b);//}ll gcd (ll a,ll b) {if (a==0) return 1; if (a<0) return gcd (-A,B); while (b) {ll t=a%b; a=b; b=t; } return A; ll Pollard_rho (ll X,ll c) {ll i=1,k=2;ll X0=rand ()%x,y=x0;while (1) {i++;x0= (Mul_mod (x0,x0,x) +c)%x;ll d=gcd (y-x0,x); if ( D>1&&D<X) return d;if (y==x0) return x;if (i==k) {y=x0;k+=k;}}} void Findfac (ll N) {if (! Miller_rabin (n)) {Prim[tot++]=n;return;} ll P=n;while (p>=n) P=pollard_rho (P,rand ()% (n-1) +1); Findfac (P); Findfac (n/p);} int main () {Srand (Time (NULL)), scanf ("%d", &t), while (t--) {scanf ("%lld", &n); Miller_rabin (n)) {printf ("prime\n"); continue;} TOT=0;FINDFAC (n); ans=prim[0];for (int i=1;i<tot;i++) {ans=min (ans,prim[i]);} printf ("%lld\n", ans);}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 1181 large integers as prime numbers and mass factor for large integers-number theory-(Miller_rabin+pollard_rho)