Pollard_rho qualitative factor decomposition algorithm depends on Miller_Rabbin algorithm determines large prime numbers , did not learn can look at, it can also be used as a template
Pollard_rho to talk about the idea of the algorithm:
The basic process quality factor of n - is first determined whether n is a prime number, if it is not in accordance with a pseudo-random number generation procedure to generate a random number sequence, to the random number is determined for each generation and n is prime, if coprime next try a random number. If not then it is coprime well divisors of p, and recursively solving p n / p factor. If n is a prime number n returns directly to its prime factors.
Pollard rho algorithm principle is that two integers a and b are obtained by some method, to be decomposed and a large integer is n, the calculation p = gcd (ab, n), until p is not 1, or a, b occurs until the cycle . And then determine whether p is n, if p = n is true, then return n is a prime number, otherwise p is a factor of n, then we can recursively compute Pollard (p) and Pollard (n / p), so we can find all the prime factors of n.
Specific operation, we usually calculated iterating the calculation value of a and b using the function x2 = x1 * x1 + c, in practice, usually taken c is 1, i.e., b = a * a + 1, the next calculation, the assigned value of b a, again using the above formula to calculate a new value of b, when a, b cycles occurs, to exit the determination.
In the actual calculation, values of a and b will eventually appeared one cycle, and these values are then connected by a smooth curve can be approximately regarded as a type of ρ.
For Pollard rho, which can be found in the time complexity of O (sqrt (p)) is a small factor p n, the efficiency can still be seen, but for a small factor, factor of great value for large integer n , Pollard rho algorithm efficiency is still not very good
Why would we take the difference between the two random numbers?
For a large integer n, we take any of a number of prime factors is such that the probability is very small, but if the number of taken two and
Their difference is that the probability of the n-factor would be increased, if x1 and x2 taken so gcd (abs (x1-x2), n)> 1 even higher probability, which is thought Pollard-Rho algorithm. (Increasing the probability is increased because the number of combinations)
Why to use Miller_Rabbin algorithm determines large prime numbers ?
Because the final result is the product of all prime factors of n (in the form of this product will only have one), then it must determine if a number is not a prime number, with Miller_Rabbin algorithm determines large prime numbers to determine if faster than conventional methods
Code:
1 #include<stdio.h> 2 #include<string.h> 3 #include<iostream> 4 #include<algorithm> 5 #include<queue> 6 #include<map> 7 #include<vector> 8 #include<math.h> 9 #define mem(a,x) memset(a,x,sizeof(a)) 10 using namespace std; 11 typedef long long LL; 12 const int maxn=50005; 13 const int mod=26; 14 const int INF = 0x3f3f3f3f ; 15 const int Times = 10 ; 16 const int N = 5500 ; . 17 LL CT, CNT; 18 is LL FAC [N], NUM [N]; . 19 LL GCD (LL A, LL B) / / find the greatest common divisor of two numbers 20 is { 21 is return B GCD (B, A%? B): A; 22 is } 23 is LL Multi (LL A, B LL, LL m) // quickly by 24 { 25 LL ANS = 0 ; 26 is A% = m; 27 while(b) 28 { 29 if(b & 1) 30 { 31 ans = (ans + a) % m; 32 b--; 33 } 34 b >>= 1; 35 a = (a + a) % m; 36 } 37 return ans; 38 } 39 LL pow(LL a, LL b, LL m) //快速幂 40 { 41 LL ans = 1; 42 a %= m; 43 while(b) 44 { 45 if(b & 1) 46 { 47 ans = multi(ans, a, m); 48 b--; 49 } 50 b >>= 1; 51 a = multi(a, a, m); 52 } 53 return ans; 54 } 55 bool Miller_Rabin(LL n) //判断n是不是素数 56 { 57 if(n == 2) return true; 58 if(n < 2 || !(n & 1)) return false; 59 LL m = n - 1; 60 int k = 0; 61 while((m & 1) == 0) 62 { 63 k++; 64 m >>= 1; 65 } 66 for(int i=0; i<Times; i++) 67 { 68 LL a = rand() % (n - 1) + 1; 69 LL x = pow(a, m, n); 70 LL y = 0; 71 for(int j=0; j<k; j++) 72 { 73 y = multi(x, x, n); 74 if(y == 1 && x != 1 && x != n - 1) return false; 75 x = y; 76 } 77 if(y != 1) return false; 78 } 79 return true; 80 } 81 LL pollard_rho(LL n, LL c) //大整数分解 82 { 83 LL i = 1, k = 2; 84 LL x = rand() % (n - 1) + 1; 85 LL y = x; 86 while(true) 87 { 88 i++; 89 x = (multi(x, x, n) + c) % n; 90 LL d = gcd((y - x + n) % n, n); 91 if(1 < d && d < n) return d; 92 if(y == x) return n; 93 if(i == k) 94 { 95 y = x; 96 k <<= 1; 97 } 98 } 99 } 100 void find(LL n, int c) //Recursive search large integer n is prime factors 101 { 102 IF (n == . 1 ) return ; 103 IF (Miller_Rabin (n)) 104 { 105 FAC [CT ++] = n; 106 return ; 107 } 108 LL P = n; 109 K = LL C; 110 the while (P> = n-) pollard_rho P = (P, C-- ); 111 Find (P, K); 112 Find (n-/ P, K); 113 } 114 int main () 115 { 116 LL n; 117 while(cin>>n) 118 { 119 ct = 0; 120 find(n, 120); 121 sort(fac, fac + ct); 122 num[0] = 1; 123 int k = 1; 124 for(int i=1; i<ct; i++) 125 { 126 if(fac[i] == fac[i-1]) 127 ++num[k-1]; 128 else 129 { 130 num[k] = 1; 131 fac[k++] = fac[i]; 132 } 133 } 134 cnt = k; 135 for(int i=0; i<cnt; i++) 136 cout<<fac[i]<<"^"<<num[i]<<" "; 137 cout<<endl; 138 } 139 return 0; 140 }