POJ 1181 large integers as prime numbers and mass factor for large integers-number theory-(Miller_rabin+pollard_rho)

Source: Internet
Author: User

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&GT;1&AMP;&AMP;D&LT;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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.