随机素数测试(Miller_Rabin算法)和求整数素因子(Pollard_rho算法)

POJ1811

给一个大数,判断是否是素数,如果不是素数,打印出它的最小质因数

随机素数测试(Miller_Rabin算法)

求整数素因子(Pollard_rho算法)

科技题

  1 #include<cstdlib>
  2 #include<cstdio>
  3 const int maxn=10005;
  4 const int S=20;
  5 int tot;
  6 long long n;
  7 long long factor[maxn];
  8 long long muti_mod(long long a,long long b,long long c)
  9 {
 10     //(a*b) mod c a,b,c<2^63
 11     a%=c;
 12     b%=c;
 13     long long ret=0;
 14     while(b)
 15     {
 16         if(b&1)
 17         {
 18             ret+=a;
 19             if(ret>=c) ret-=c;
 20         }
 21         a<<=1;
 22         if(a>=c) a-=c;
 23         b>>=1;
 24     }
 25     return ret;
 26 }
 27 long long pow_mod(long long x,long long n,long long mod)
 28 {
 29     //x^n mod c
 30     if(n==1) return x%mod;
 31     int bit[90],k=0;
 32     while(n)
 33     {
 34         bit[k++]=n&1;
 35         n>>=1;
 36     }
 37     long long ret=1;
 38     for(k=k-1;k>=0;k--)
 39     {
 40         ret=muti_mod(ret,ret,mod);
 41         if(bit[k]==1) ret=muti_mod(ret,x,mod);
 42     }
 43     return ret;
 44 }
 45 bool check(long long a,long long n,long long x,long long t)
 46 {
 47     long long ret=pow_mod(a,x,n),last=ret;
 48     for(int i=1;i<=t;i++)
 49     {
 50         ret=muti_mod(ret,ret,n);
 51         if(ret==1&&last!=1&&last!=n-1) return 1;
 52         last=ret;
 53     }
 54     if(ret!=1) return 1;
 55     return 0;
 56 }
 57 bool Miller_Rabin(long long n)
 58 {
 59     long long x=n-1,t=0;
 60     while((x&1)==0) x>>=1,t++;
 61     bool flag=1;
 62     if(t>=1&&(x&1)==1)
 63     {
 64         for(int k=0;k<S;k++)
 65         {
 66             long long a=rand()%(n-1)+1;
 67             if(check(a,n,x,t)) {flag=1;break;}
 68             flag=0;
 69         }
 70     }
 71     if(flag==0||n==2) return 0;
 72     return 1;
 73 }
 74 long long gcd(long long a,long long b)
 75 {
 76     if(a==0) return 1;
 77     if(a<0) return gcd(-a,b);
 78     while(b)
 79     {
 80         long long t=a%b;a=b;b=t;
 81     }
 82     return a;
 83 }
 84 long long Pollard_rho(long long x,long long c)
 85 {
 86     long long i=1,x0=rand()%x,y=x0,k=2;
 87     while(1)
 88     {
 89         i++;
 90         x0=(muti_mod(x0,x0,x)+c)%x;
 91         long long d=gcd(y-x0,x);
 92         if(d!=1&&d!=x) return d;
 93         if(y==x0) return x;
 94         if(i==k)
 95         {
 96             y=x0;
 97             k+=k;
 98         }
 99     }
100 }
101 void findfac(long long n)  //递归分解质因数
102 {
103     if(!Miller_Rabin(n))
104     {
105         factor[tot++]=n;
106         return;
107     }
108     long long p=n;
109     while(p>=n) p=Pollard_rho(p,rand()%(n-1)+1);
110     findfac(p);
111     findfac(n/p);
112 }
113 int main()
114 {
115     int T;
116     scanf("%d",&T);
117     while(T--)
118     {
119         scanf("%I64d",&n);
120         if(!Miller_Rabin(n))
121         {
122             printf("Prime\n");
123             continue;
124         }
125         tot=0;
126         findfac(n);
127         long long ans=factor[0];
128         for(int i=1;i<tot;i++)
129             if(factor[i]<ans) ans=factor[i];
130         printf("%I64d\n",ans);
131     }
132     return 0;
133 }

原文地址:https://www.cnblogs.com/aininot260/p/9574471.html

时间: 2024-10-13 02:46:47

随机素数测试(Miller_Rabin算法)和求整数素因子(Pollard_rho算法)的相关文章

HDU2138 随机素数测试 Miller-Rabin算法

题目描述 Give you a lot of positive integers, just to find out how many prime numbers there are.. In each case, there is an integer N representing the number of integers to find. Each integer won’t exceed 32-bit signed integer, and each of them won’t be

HDU1164_Eddy&amp;#39;s research I【Miller Rabin素数测试】【Pollar Rho整数分解】

Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6664    Accepted Submission(s): 3997 Problem Description Eddy's interest is very extensive, recently he is interested in prime

HDU1164_Eddy&#39;s research I【Miller Rabin素数测试】【Pollar Rho整数分解】

Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6664    Accepted Submission(s): 3997 Problem Description Eddy's interest is very extensive, recently he is interested in prime

POJ2429_GCD &amp; LCM Inverse【Miller Rabin素数测试】【Pollar Rho整数分解】

GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9756Accepted: 1819 Description Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and the least common multiple (LCM) of a and b.

POJ1811_Prime Test【Miller Rabin素数测试】【Pollar Rho整数分解】

Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 Case Time Limit: 4000MS Description Given a big integer number, you are required to find out whether it's a prime number. Input The first line contains the num

poj1811 Prime Test,随机素数测试

Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 24514   Accepted: 5730 Case Time Limit: 4000MS Description Given a big integer number, you are required to find out whether it's a prime number. Input The first line contains the

【算法日记】贝尔曼-福德算法

如上图使用Dijkstra算法将无法获取到最短路径 1.A->C->D   5 2.A->B...没有 最近路径为5.但是实际上B->C的路径为-2. A->B->C->D的最短开销为3 Dijkstra算法无法判断含负权边的图的最短路.如果遇到负权,在没有负权回路存在时(负权回路的含义是,回路的权值和为负.)即便有负权的边,也可以采用贝尔曼-福德算法算法正确求出最短路径. 算法实现 1 def bellman_ford( graph, source ): 2 3

数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test

Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 29046   Accepted: 7342 Case Time Limit: 4000MS Description Given a big integer number, you are required to find out whether it's a prime number. Input The first line contains the

Miller_Rabin素数测试算法模板对比

昨天在USACO做了一道判断素数的题,就想着学习一下Miller_Rabin素数测试算法,在网上找到两种模版,第一种十分简洁,运行速度也很快,但是会判错极少的几个非素数:第二种比较麻烦,运行速度很慢,所以我便想找到第一种模版不能判断的非素数特判一下,结果用了一天,电脑只找到10^8以下的,10^9内还有2个没找到,但正确的模版运行速度太慢,我的电脑又太渣,耗不起时间了,姑且先这样,等以后有深入理解有更好的方法再更新一下. 第一种:源自吉林大学ACM模版 刚开始用的是随机数测试,我想到以前了解过只