Prime Test
Time limit:6000 ms |
|
Memory limit:65536 K |
Total submissions:24514 |
|
Accepted:5730 |
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
Miller_rabin Algorithm
# Include <cstdio> # include <cstring> # include <cmath> # include <ctime> # include <iostream> # include <algorithm> using namespace STD; typedef long ll; // rand (void) returns a random integer between [0, rand_max. // Random (n) returns a random integer between [0, N] ll random (ll n) {return (LL) (double) rand () /rand_max * n + 0.5);} inline ll mul_mod (ll a, LL B, ll c) {ll res = 0; A % = C; B % = C; for (; B; B >>= 1, A = (a <1) % C) {If (B & 1) RES = (RES + a) % C ;} return res;} ll pow_mod (ll a, LL B, ll c) {ll res = 1; for (; B; B> = 1, A = mul_mod (, a, c) {If (B & 1) RES = mul_mod (Res, a, c);} return res;} bool check (LL A, ll N, ll X, ll t) {ll ret = Pow_mod (A, X, n); ll last = ret; For (INT I = 1; I <= T; ++ I) {ret = mul_mod (Ret, RET, n); If (ret = 1 & last! = 1 & last! = N-1) return true; last = ret;} If (Ret! = 1) return true; else return false;} const int n = 8; bool miller_rabin (ll n) {If (n <2) return false; If (n = 2) return true; If (N & 1) = 0) return false; ll x = n-1; ll T = 0; while (X & 1) = 0) {x> = 1; t ++;} For (INT I = 0; I <n; ++ I) {ll A = random (X-2) + 1; if (check (A, n, x, t) return false;} return true;} ll factor [100]; int tol; ll gcd (ll a, LL B) {return B? Gcd (B, A % B): A> = 0? A:-A;} ll pollard_rov (ll x, ll c) {ll I = 1, K = 2; ll X0 = random (X-2) + 1; ll 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, int K) {If (n = 1) return; If (miller_rabin (N) {factor [tol ++] = N; return ;} ll p = N; int c = K; while (P> = N) P = pollard_rov (p, c --); findfac (P, K); findfac (N/P, k);} int main () {int t; ll N; // srand (Time (null); // remove this sentence from G ++ on poj, A sad story .. Scanf ("% d", & T); While (t --) {scanf ("% LLD", & N); If (miller_rabin (n )) {printf ("prime \ n") ;}else {Tol = 0; findfac (n, 107); LL ans = factor [0]; for (INT I = 1; I <tol; ++ I) if (ANS> factor [I]) ans = factor [I]; printf ("% LLD \ n", ANS );}} return 0 ;}
Poj1811 prime test, random prime number Test