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
| #include <bits/stdc++.h> using namespace std; typedef long long ll; ll pr; ll pmod(ll a, ll b, ll p) { return (a * b - (ll)((long double)a / p * b) * p + p) % p; } ll gmod(ll a, ll b, ll p) { ll res = 1; while (b) { if (b & 1) res = pmod(res, a, p); a = pmod(a, a, p); b >>= 1; } return res; } inline ll gcd(ll a, ll b) { if (!a) return b; if (!b) return a; int t = __builtin_ctzll(a | b); a >>= __builtin_ctzll(a); do { b >>= __builtin_ctzll(b); if (a > b) { ll t = b; b = a, a = t; } b -= a; } while (b); return a << t; } bool Miller_Rabin(ll n) { if (n == 46856248255981ll || n < 2) return false; if (n == 2 || n == 3 || n == 7 || n == 61 || n == 24251) return true; if (!(n & 1) || !(n % 3) || !(n % 61) || !(n % 24251)) return false; ll m = n - 1, k = 0; while (!(m & 1)) k++, m >>= 1; for (int i = 1; i <= 20; ++i) { ll a = rand() % (n - 1) + 1, x = gmod(a, m, n), y; for (int j = 1; j <= k; ++j) { y = pmod(x, x, n); if (y == 1 && x != 1 && x != n - 1) return 0; x = y; } if (y != 1) return 0; } return 1; } ll Pollard_Rho(ll x) { ll n = 0, m = 0, t = 1, q = 1, c = rand() % (x - 1) + 1; for (ll k = 2;; k <<= 1, m = n, q = 1) { for (ll i = 1; i <= k; ++i) { n = (pmod(n, n, x) + c) % x; q = pmod(q, abs(m - n), x); } t = gcd(x, q); if (t > 1) return t; } } map<long long, int> m; void fid(ll n) { if (n == 1) return; if (Miller_Rabin(n)) { pr = max(pr, n); m[n]++; return; } ll p = n; while (p >= n) p = Pollard_Rho(p); fid(p); fid(n / p); } int main() { int T; ll n; scanf("%d", &T); while (T--) { m.clear(); scanf("%lld", &n); pr = 0; fid(n); if (pr == n) puts("Prime"); else { printf("%lld\n", pr); for (map<long long, int>::iterator c = m.begin(); c != m.end();) { printf("%lld^%d", c->first, c->second); if ((++c) != m.end()) printf(" * "); } printf("\n"); } } return 0; }
|