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
| i64 mul(i64 a, i64 b, i64 m) { return static_cast<__int128>(a) * b % m; } i64 power(i64 a, i64 b, i64 m) { i64 res = 1 % m; for (; b; b >>= 1, a = mul(a, a, m)) if (b & 1) res = mul(res, a, m); return res; } bool isprime(i64 n) { if (n < 2) return false; static constexpr int A[] = {2, 3, 5, 7, 11, 13, 17, 19, 23}; int s = __builtin_ctzll(n - 1); i64 d = (n - 1) >> s; for (auto a : A) { if (a == n) return true; i64 x = power(a, d, n); if (x == 1 || x == n - 1) continue; bool ok = false; for (int i = 0; i < s - 1; ++i) { x = mul(x, x, n); if (x == n - 1) { ok = true; break; } } if (!ok) return false; } return true; }
std::vector<i64> factorize(i64 n) { std::vector<i64> p; std::function<void(i64)> f = [&](i64 n) { if (n <= 10000) { for (int i = 2; i * i <= n; ++i) for (; n % i == 0; n /= i) p.push_back(i); if (n > 1) p.push_back(n); return; } if (isprime(n)) { p.push_back(n); return; } auto g = [&](i64 x) { return (mul(x, x, n) + 1) % n; }; i64 x0 = 2; while (true) { i64 x = x0; i64 y = x0; i64 d = 1; i64 power = 1, lam = 0; i64 v = 1; while (d == 1) { y = g(y); ++lam; v = mul(v, std::abs(x - y), n); if (lam % 127 == 0) { d = std::gcd(v, n); v = 1; } if (power == lam) { x = y; power *= 2; lam = 0; d = std::gcd(v, n); v = 1; } } if (d != n) { f(d); f(n / d); return; } ++x0; } }; f(n); std::sort(p.begin(), p.end()); return p; }
using factor = std::pair<i64, int>;
std::vector<i64> GetDivisors(const std::vector<i64>& factors) { std::unordered_map<i64, int> cnt; for (auto fi : factors) {cnt[fi] += 1;} std::vector<factor> fac_cnt(cnt.begin(), cnt.end()); std::vector<i64> divisors = {1}; for (auto &p : fac_cnt) { int sz = divisors.size(); for (int i = 0; i < sz; i++) { i64 cur = divisors[i]; for (int j = 0; j < p.second; j++) { cur *= p.first; divisors.push_back(cur); } } } return divisors; }
|