This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub ngthanhtrung23/ACM_Notebook_new
#include "Pollard_factorize.h" #include "../Prime/Sieve.h" // Tested: https://www.spoj.com/problems/NUMDIV/ int64_t cnt_divisors(int64_t n) { assert(n > 0); auto ps = factorize(n); int cnt_ps = ps.size(); int i = 0; int64_t res = 1; while (i < cnt_ps) { int j = i; while (j+1 < cnt_ps && ps[j+1] == ps[j]) ++j; res *= j - i + 2; i = j + 1; } return res; } // Count divisors Using Segmented Sieve O(sieve(sqrt(R)) + (R-L)*log) {{{ // Returns vector of length (r - l + 1), where the i-th element is number of // divisors of i - l vector<int> cnt_divisors_segmented_sieve(int l, int r) { int s = sqrt(r + 0.5); vector<int> primes; auto newPrime = [&] (int p) { primes.push_back(p); }; sieve(s, newPrime); vector<int> cnt(r - l + 1, 1), cur(r - l + 1); std::iota(cur.begin(), cur.end(), l); for (int p : primes) { if (p > r) break; int u = (l + p - 1) / p * p; for (int i = u; i <= r; i += p) { int k = 0; while (cur[i-l] % p == 0) cur[i-l] /= p, ++k; cnt[i - l] *= k + 1; } } for (int i = l; i <= r; ++i) { if (cur[i-l] > 1) cnt[i-l] *= 2; } return cnt; } // }}}
#line 1 "Math/NumberTheory/Pollard_factorize.h" // Copied from https://judge.yosupo.jp/submission/61447 // O(N^0.25) // // Tested: // - (up to 10^18; 200 tests) https://judge.yosupo.jp/problem/factorize // - https://oj.vnoi.info/problem/icpc21_beta_l // - https://www.spoj.com/problems/FACT0/ // // Pollard {{{ using ll = long long; using ull = unsigned long long; using ld = long double; ll mult(ll x, ll y, ll md) { ull q = (ld)x * y / md; ll res = ((ull)x * y - q * md); if (res >= md) res -= md; if (res < 0) res += md; return res; } ll powMod(ll x, ll p, ll md) { if (p == 0) return 1; if (p & 1) return mult(x, powMod(x, p - 1, md), md); return powMod(mult(x, x, md), p / 2, md); } bool checkMillerRabin(ll x, ll md, ll s, int k) { x = powMod(x, s, md); if (x == 1) return true; while(k--) { if (x == md - 1) return true; x = mult(x, x, md); if (x == 1) return false; } return false; } bool isPrime(ll x) { if (x == 2 || x == 3 || x == 5 || x == 7) return true; if (x % 2 == 0 || x % 3 == 0 || x % 5 == 0 || x % 7 == 0) return false; if (x < 121) return x > 1; ll s = x - 1; int k = 0; while(s % 2 == 0) { s >>= 1; k++; } if (x < 1LL << 32) { for (ll z : {2, 7, 61}) { if (!checkMillerRabin(z, x, s, k)) return false; } } else { for (ll z : {2, 325, 9375, 28178, 450775, 9780504, 1795265022}) { if (!checkMillerRabin(z, x, s, k)) return false; } } return true; } ll gcd(ll x, ll y) { return y == 0 ? x : gcd(y, x % y); } void pollard(ll x, vector<ll> &ans) { if (isPrime(x)) { ans.push_back(x); return; } ll c = 1; while(true) { c = 1 + get_rand(x - 1); auto f = [&](ll y) { ll res = mult(y, y, x) + c; if (res >= x) res -= x; return res; }; ll y = 2; int B = 100; int len = 1; ll g = 1; while(g == 1) { ll z = y; for (int i = 0; i < len; i++) { z = f(z); } ll zs = -1; int lft = len; while(g == 1 && lft > 0) { zs = z; ll p = 1; for (int i = 0; i < B && i < lft; i++) { p = mult(p, abs(z - y), x); z = f(z); } g = gcd(p, x); lft -= B; } if (g == 1) { y = z; len <<= 1; continue; } if (g == x) { g = 1; z = zs; while(g == 1) { g = gcd(abs(z - y), x); z = f(z); } } if (g == x) break; assert(g != 1); pollard(g, ans); pollard(x / g, ans); return; } } } // return list of all prime factors of x (can have duplicates) vector<ll> factorize(ll x) { vector<ll> ans; for (ll p : {2, 3, 5, 7, 11, 13, 17, 19}) { while(x % p == 0) { x /= p; ans.push_back(p); } } if (x != 1) { pollard(x, ans); } sort(ans.begin(), ans.end()); return ans; } // return pairs of (p, k) where x = product(p^k) vector<pair<ll, int>> factorize_pk(ll x) { auto ps = factorize(x); ll last = -1, cnt = 0; vector<pair<ll, int>> res; for (auto p : ps) { if (p == last) ++cnt; else { if (last > 0) res.emplace_back(last, cnt); last = p; cnt = 1; } } if (cnt > 0) { res.emplace_back(last, cnt); } return res; } vector<ll> get_all_divisors(ll n) { auto pks = factorize_pk(n); vector<ll> res; function<void(int, ll)> gen = [&] (int i, ll prod) { if (i == static_cast<int>(pks.size())) { res.push_back(prod); return; } ll cur_power = 1; for (int cur = 0; cur <= pks[i].second; ++cur) { gen(i+1, prod * cur_power); cur_power *= pks[i].first; } }; gen(0, 1LL); sort(res.begin(), res.end()); return res; } // }}} #line 1 "Math/Prime/Sieve.h" // F is called for each prime // Sieve (odd only + segmented) {{{ template<typename F> void sieve(int MAX, F func) { const int S = sqrt(MAX + 0.5); vector<char> sieve(S + 1, true); vector<array<int, 2>> cp; for (int i = 3; i <= S; i += 2) { if (!sieve[i]) continue; cp.push_back({i, (i * i - 1) / 2}); for (int j = i * i; j <= S; j += 2 * i) sieve[j] = false; } func(2); vector<char> block(S); int high = (MAX - 1) / 2; for (int low = 0; low <= high; low += S) { fill(block.begin(), block.end(), true); for (auto &i : cp) { int p = i[0], idx = i[1]; for (; idx < S; idx += p) block[idx] = false; i[1] = idx - S; } if (low == 0) block[0] = false; for (int i = 0; i < S && low + i <= high; i++) if (block[i]) { func((low + i) * 2 + 1); } }; } // }}} #line 3 "Math/NumberTheory/cnt_divisors.h" // Tested: https://www.spoj.com/problems/NUMDIV/ int64_t cnt_divisors(int64_t n) { assert(n > 0); auto ps = factorize(n); int cnt_ps = ps.size(); int i = 0; int64_t res = 1; while (i < cnt_ps) { int j = i; while (j+1 < cnt_ps && ps[j+1] == ps[j]) ++j; res *= j - i + 2; i = j + 1; } return res; } // Count divisors Using Segmented Sieve O(sieve(sqrt(R)) + (R-L)*log) {{{ // Returns vector of length (r - l + 1), where the i-th element is number of // divisors of i - l vector<int> cnt_divisors_segmented_sieve(int l, int r) { int s = sqrt(r + 0.5); vector<int> primes; auto newPrime = [&] (int p) { primes.push_back(p); }; sieve(s, newPrime); vector<int> cnt(r - l + 1, 1), cur(r - l + 1); std::iota(cur.begin(), cur.end(), l); for (int p : primes) { if (p > r) break; int u = (l + p - 1) / p * p; for (int i = u; i <= r; i += p) { int k = 0; while (cur[i-l] % p == 0) cur[i-l] /= p, ++k; cnt[i - l] *= k + 1; } } for (int i = l; i <= r; ++i) { if (cur[i-l] > 1) cnt[i-l] *= 2; } return cnt; } // }}}