Prime Test
Time limit:6000 ms |
|
Memory limit:65536 K |
Total submissions:29046 |
|
Accepted:7342 |
Case time limit:4000 Ms |
Description
Given a big integer number, you are required to find out whether it's a prime number.
Input
The first line contains the number of test cases T (1 <= T <= 20), then the following T lines each contains an integer number N (2 <= n <254 ).
Output
For each test case, if n is a prime number, output a line containing the word "prime", otherwise, output a line containing the smallest prime factor of N.
Sample Input
2510
Sample output
Prime2
Source
Poj monthly
Mean:
.
Analyze:
The input N is very large, and we cannot use the sieve method to obtain the prime number. In this case, the miller_rabin algorithm is particularly important.
If n is not a prime number, we need to perform prime factor decomposition. The same problem is that N is very large. We cannot use Trial Division to perform prime factor decomposition, so it must be TLE. In this case, you must use the pollard_rov Algorithm for prime factor decomposition.
In fact, the miller_rabin algorithm and the pollard_rov algorithm are often used together.
Time Complexity:O (n) is generally O (N)
Source code:
// Memory time // 1347 K 0 Ms //: snarl_jsb # include <algorithm> # include <cstdio> # include <cstring> # include <cstdlib> # include <iostream> # include <vector> # include <queue> # include <Stack> # include <map >#include <string ># include <climits> # include <cmath> # define n 1000010 # define ll long longusing namespace STD; //************************************** * *********************** // The miller_rabin algorithm is used to test the prime number and the speed is fast, in addition, you can determine the value of <2 ^ 63 //************************************ * *************************** Const int S = 20; // number of random algorithm determination times. The larger the value of S, the smaller the probability of error determination. // calculate (A * B) % C. A and B are the numbers of long. Directly multiplying them may overflow. // a, B, c <2 ^ 63 long mult_mod (long a, long B, long long c) {A % = C; B % = C; long ret = 0; while (B) {If (B & 1) {RET + =; RET % = C;} A <= 1; if (a> = C) a % = C; B >>= 1;} return ret ;} // calculate x ^ n % CLONG long pow_mod (long X, long N, long mod) // x ^ n % c {if (N = 1) return X % MOD; X % = MOD; long TMP = x; long ret = 1; while (n) {If (N & 1) ret = mult_mod (Ret, TMP, MoD); TMP = mult_mod (TMP, TMP, MoD); N >>= 1;} return ret;} // A-based, n-1 = x * 2 ^ t a ^ (n-1) = 1 (mod n) verify whether N is a combination number. // it must be a combination. True is returned, not necessarily falsebool check (long, long long N, long X, long T) {long ret = pow_mod (A, X, n); long last = ret; For (INT I = 1; I <= T; I ++) {ret = mult_mod (Ret, RET, n); If (ret = 1 & last! = 1 & last! = N-1) return true; // sum last = ret;} If (Ret! = 1) return true; return false;} // miller_rabin () algorithm prime number determination // returns true if it is a prime number. (It may be a Pseudo Prime Number, but the probability is extremely small) // false is returned for the sum of values; bool miller_rabin (long n) {If (n <2) return false; if (n = 2) return true; If (N & 1) = 0) return false; // even long x = n-1; long T = 0; while (X & 1) = 0) {x >>=1; t ++ ;}for (INT I = 0; I <s; I ++) {long a = rand () % (n-1) + 1; // rand () requires stdlib. h header file if (check (A, n, x, t) return false; // sum} return true ;} //****************** //* **************************************** * ****** Long factor [100]; // prime factor decomposition result (unordered upon return) int tol; // Number of prime factors. The array small mark starts from 0 long gcd (long a, long B) {if (a = 0) return 1; if (a <0) return gcd (-, b); while (B) {long T = A % B; A = B; B = T;} return a;} long pollard_rock (long X, long long c) {long I = 1, K = 2; long X0 = rand () % x; long y = x0; while (1) {I ++; x0 = (mult_mod (x0, x0, x) + C) % x; long d = gcd (y-x0, x); If (D! = 1 & D! = X) return D; If (y = x0) return X; if (I = k) {Y = x0; k + = K ;}}} // perform prime factor decomposition on N void findfac (long n) {If (miller_rabin (N) // prime number {factor [tol ++] = N; return ;} long long P = N; while (P> = N) P = maid (p, Rand () % (n-1) + 1); findfac (P ); findfac (N/P);} int main () {// srand (Time (null); // time required. h header file // G ++ on poj cannot add this sentence long n; long T; CIN> T; while (t --) {scanf ("% i64d ", & N); If (n = 1) continue; If (miller_rabin (N) printf ("prime \ n"); else {Tol = 0; findfac (N ); long long Minn = int_max; For (INT I = 0; I <tol; I ++) {If (factor [I] <Minn) {Minn = factor [I] ;}} printf ("% i64d \ n", Minn) ;}} return 0 ;}
Number Theory-miller_rabin prime number test + pollard_rock algorithm factorization prime factor ---- poj 1811: prime test