1 // accepted 164 KB 422 MS 2 // similar to poj2429 big data decomposition 3 # include <cstdio> 4 # include <cstring> 5 # include <ctime> 6 # include <time. h> 7 # include <iostream> 8 # include <algorithm> 9 Using namespace STD; 10 const _ int64 INF = 1ll <60; 11 _ int64 gcd (_ int64 A ,__ int64 B) 12 {13 if (B = 0) return a; 14 return gcd (B, A % B ); 15} 16 _ int64 mult_mod (_ int64 A ,__ int64 B ,__ int64 p) 17 {18 _ int64 res = 0, temp = A % P; 1 9 while (B) 20 {21 if (B & 1) 22 {23 res + = temp; 24 if (RES> = P) RES-= P; 25} 26 temp <= 1; 27 if (temp> = P) temp-= P; 28 B >>= 1; 29} 30 return res; 31} 32 _ int64 exp_mod (_ int64 A ,__ int64 B ,__ int64 p) 33 {34 _ int64 res = 1, exp = A % P; 35 while (B> = 1) 36 {37 If (B & 1) 38 res = mult_mod (Res, exp, P); 39 exp = mult_mod (exp, p); 40 B >>= 1; 41} 42 return res; 43} 44 bool miller_rabin (_ int64 n ,__ int64 times) 45 {46 If (n = 2) return true; 47 If (n <2 |! (N & 1) return false; 48 _ int64 A, u = n-1, X, Y; 49 int T = 0; 50 while (U % 2 = 0) 51 {52 T ++; 53 U/= 2; 54} 55 srand (time (0); 56 for (INT I = 0; I <times; I ++) 57 {58 A = rand () % (n-1) + 1; 59 x = exp_mod (A, U, N); 60 for (Int J = 0; j <t; j ++) 61 {62 y = mult_mod (x, x, n); 63 If (y = 1 & X! = 1 & X! = N-1) 64 return false; 65 x = y; 66} 67 If (y! = 1) return false; 68} 69 return true; 70} 71 _ int64 pollar_rov (_ int64 N, int c) 72 {73 _ int64 X, Y, D, I = 1, K = 2; 74 srand (time (0); 75 x = rand () % (n-1) + 1; 76 y = x; 77 while (true) 78 {79 I ++; 80 x = (mult_mod (x, x, n) + C) % N; 81 d = gcd (Y-X, N ); 82 If (D> 1 & D <n) return D; 83 If (y = x) return N; 84 if (I = K) 85 {86 y = x; 87 k <= 1; 88} 89} 90} 91 _ int64 min_ans; 92 void findminfactor (_ int64 N, int C) 93 {94 If (n = 1) return; 95 If (miller_rabin (n, 10) 96 {97 If (n <min_ans) min_ans = N; 98 return; 99} 100 _ int64 P = N; 101 while (P> = N) 102 P = pollar_rov (p, c --); 103 findminfactor (p, C ); 104 findminfactor (N/P, c); 105} 106 int main () 107 {108 int t; 109 scanf ("% d", & T ); 110 while (t --) 111 {112 _ int64 N; 113 scanf ("% i64d", & N); 114 If (miller_rabin (n, 10) = true) 115 {116 printf ("prime \ n"); 117} 118 else119 {120 min_ans = inf; 121 findminfactor (n, 107); 122 printf ("% i64d \ n ", min_ans); 123} 124} 125 return 0; 126}
View code
Poj1181 big data decomposition