Prime Test
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
2
5
10
Sample Output
Prime
2
Source
题目类型:Miller_Rabin测试和Pollard_Rho大整数分解
算法分析:先判断n是否是素数,如果是则直接输出”Prime”,反之则分解n大整数,生成n所有的素因子,然后找到最小的那一个即可,注意srand()要注销掉,否则会RE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> #define lson rt << 1, l, m #define rson rt << 1 | 1, m + 1, r using namespace std; const int INF = 0x7FFFFFFF; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int MOD = 7; const int maxn = 10000 + 66; long long factor[maxn], fac_len; const int testnum = 8; //随机算法判定次数,一般8~10就够了 // 计算ret = (a*b)%c a,b,c < 2^63 long long mult_mod (long long a,long long b,long long mod) { a %= mod; b %= mod; long long ans = 0; long long temp = a; while (b) { if (b & 1) { ans += temp; if (ans > mod) ans -= mod;//直接取模慢很多 } temp <<= 1; if (temp > mod) temp -= mod; b >>= 1; } return ans; } long long pow_mod (long long a,long long n,long long mod) { long long ans = 1; long long temp = a % mod; while (n) { if (n & 1) ans = mult_mod (ans, temp, mod); temp = mult_mod (temp, temp, mod); n >>= 1; } return ans; } // 通过 a^(n-1)=1(mod n)来判断n是不是素数 // n-1 = x*2^t 中间使用二次判断 // 是合数返回true, 不一定是合数返回false bool check (long long a, long long n, long long x, long long t) { long long ans = pow_mod (a, x, n); long long last = ans; for(int i = 1; i <= t; i++) { ans = mult_mod (ans, ans, n); if(ans == 1 && last != 1 && last != n - 1) return true;//合数 last = ans; } if(ans != 1) return true; else return false; } //************************************************** // Miller_Rabin算法 // 是素数返回true,(可能是伪素数) // 不是素数返回false //************************************************** bool Miller_Rabin (long long n) { if (n < 2) return false; if (n == 2) return true; if ((n&1) == 0) return false;//偶数 long long x = n - 1; long long t = 0; while ((x&1) == 0) { x >>= 1; t++; } // srand (time (NULL)); for (int i = 0; i < testnum; i++) { long long a = rand () % (n - 1) + 1; if (check (a, n, x, t)) return false; } return true; } long long gcd (long long a, long long b) { if (a == 0) return 1; if (a < 0) return gcd (-a, b); while (b) { long long t = a % b; a = b; b = t; } return a; } long long Pollard_Rho (long long x, long long c) { long long i = 1, k = 2; long long x0 = rand () % x; long long y = x0; while (1) { i++; x0 = (mult_mod (x0, x0, x) + c) % x; long 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; } } } //对n进行素因子分解 void FindFac (long long n) { if (Miller_Rabin (n))//素数 { factor[fac_len++] = n; return ; } long long p = n; while (p >= n) p = Pollard_Rho (p, rand () % (n - 1) + 1); FindFac (p); FindFac (n / p); } int main() { // freopen ("aaa.txt", "r", stdin); long long t; scanf ("%lld", &t); while (t--) { long long n; scanf ("%lld", &n); if (Miller_Rabin (n)) printf ("Prime\n"); else { fac_len = 0; FindFac (n); long long minval = INF; for (long long i = 0; i < fac_len; i++) minval = min (minval, factor[i]); printf ("%lld\n", minval); } } return 0; } |
- « 上一篇:poj1808
- poj1845:下一篇 »