ACM_Notebook_new

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub ngthanhtrung23/ACM_Notebook_new

:heavy_check_mark: Math/tests/cnt_divisors_stress.test.cpp

Depends on

Code

#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"

#include "../../template.h"
#include "../NumberTheory/cnt_divisors.h"
#include "../multiplicative_function.h"

const int N = 1000000;
MultiplicativeFunction<N + 1> mf;
auto divisors = mf.divisors();

#include "../multiplicative_functions_linear.h"

void solve() {
    linear_sieve::linear_sieve_divisors(N + 1);
    for (int i = 1; i <= N; ++i) {
        assert(divisors[i] == cnt_divisors(i));
        assert(divisors[i] == linear_sieve::cnt_divisors[i]);
    }
    cout << "Hello World\n";
}
#line 1 "Math/tests/cnt_divisors_stress.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"

#line 1 "template.h"
#include <bits/stdc++.h>
using namespace std;

#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)

#define DEBUG(x) { cout << #x << " = "; cout << (x) << endl; }
#define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
#define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; }

#define sqr(x) ((x) * (x))

// For printing pair, container, etc.
// Copied from https://quangloc99.github.io/2021/07/30/my-CP-debugging-template.html
template<class U, class V> ostream& operator << (ostream& out, const pair<U, V>& p) {
    return out << '(' << p.first << ", " << p.second << ')';
}

template<class Con, class = decltype(begin(declval<Con>()))>
typename enable_if<!is_same<Con, string>::value, ostream&>::type
operator << (ostream& out, const Con& con) {
    out << '{';
    for (auto beg = con.begin(), it = beg; it != con.end(); it++) {
        out << (it == beg ? "" : ", ") << *it;
    }
    return out << '}';
}
template<size_t i, class T> ostream& print_tuple_utils(ostream& out, const T& tup) {
    if constexpr(i == tuple_size<T>::value) return out << ")"; 
    else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); 
}
template<class ...U> ostream& operator << (ostream& out, const tuple<U...>& t) {
    return print_tuple_utils<0, tuple<U...>>(out, t);
}

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
long long get_rand(long long r) {
    return uniform_int_distribution<long long> (0, r-1)(rng);
}

template<typename T>
vector<T> read_vector(int n) {
    vector<T> res(n);
    for (int& x : res) cin >> x;
    return res;
}

void solve();

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    solve();
    return 0;
}
#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;
}
// }}}
#line 1 "Math/multiplicative_function.h"
// NOTE: calculate upto N-1
//
// Multiplicative function {{{
template<int N>
struct MultiplicativeFunction {
    // Init sieve and pk
    MultiplicativeFunction() {
        // Init sieve
        for (int i = 2; i*i < N; i++) {
            if (!sieve[i]) {
                for (int j = i*i; j < N; j += i) {
                    sieve[j] = i;
                }
            }
        }

        // Init pk
        for (int i = 2; i < N; i++) {
            if (!sieve[i]) {
                pk[i] = {i, 1};
            } else {
                int p = sieve[i];

                if (pk[i/p].first == p) { // i = p^k
                    pk[i] = {p, pk[i/p].second + 1};
                } else {
                    pk[i] = {-1, 0};
                }
            }
        }
    }

    // Tested: https://cses.fi/problemset/task/1713
    array<int, N> divisors() {
        array<int, N> res;
        res[1] = 1;

        for (int i = 2; i < N; i++) {
            if (pk[i].first > 0) {  // i = p^k
                res[i] = pk[i].second + 1;
            } else {
                // i = u * v, gcd(u, v) = 1
                int u = i, v = 1;
                int p = sieve[i];
                while (u % p == 0) {
                    u /= p;
                    v *= p;
                }
                res[i] = res[u] * res[v];
            }
        }

        return res;
    }

    // mobius(n) = 1  if n is square-free and has *even* number of prime factors
    // mobius(n) = -1 if n is square-free and has *odd* number of of prime factors
    // mobius(n) = 0  if n is not square-free
    array<int, N> mobius() {
        array<int, N> res;
        res[1] = 1;

        for (int i = 2; i < N; ++i) {
            if (pk[i].first > 0) {  // i = p^k
                res[i] = (pk[i].second >= 2) ? 0 : -1;
            } else {
                // i = u * v, gcd(u, v) = 1
                int u = i, v = 1;
                int p = sieve[i];
                while (u % p == 0) {
                    u /= p;
                    v *= p;
                }
                res[i] = res[u] * res[v];
            }
        }
        return res;
    }

// private:
    // sieve[i] == 0 if i is prime,
    // sieve[i] = any prime factor p otherwise
    array<int, N> sieve = {0};

    // pk[i] = {p, k} if i == p^k
    // pk[i] = {-1, 0} otherwise
    array<pair<int,int>, N> pk;
};
// }}}
#line 6 "Math/tests/cnt_divisors_stress.test.cpp"

const int N = 1000000;
MultiplicativeFunction<N + 1> mf;
auto divisors = mf.divisors();

#line 1 "Math/multiplicative_functions_linear.h"
// This is only for calculating multiplicative functions
// If we need a fast sieve, see SieveFast.h
// From https://codeforces.com/blog/entry/54090
namespace linear_sieve {
const int MN = 2e7;
vector<int> primes;

int smallest_p[MN];  // smallest_p[n] = smallest prime factor of n
void linear_sieve_smallest_prime_factor(int n) {
    primes.clear();
    memset(smallest_p, 0, sizeof smallest_p);

    for (int i = 2; i < n; ++i) {
        if (!smallest_p[i]) primes.push_back(i);
        for (int j = 0; j < int(primes.size()) && i * primes[j] < n; ++j) {
            smallest_p[i * primes[j]] = primes[j];
            if (i % primes[j] == 0) break;
        }
    }
}

// Euler Phi {{{
bool is_composite[MN];
int phi[MN];

void linear_sieve_phi(int n) {
    memset(is_composite, false, sizeof is_composite);
    primes.clear();
 
    phi[1] = 1;
    for (int i = 2; i < n; ++i) {
        if (!is_composite[i]) {
            primes.push_back(i);
            phi[i] = i - 1; // i is prime
        }
        for (int j = 0; j < (int) primes.size() && i * primes[j] < n; ++j) {
            is_composite[i * primes[j]] = true;
            if (i % primes[j] == 0) {
                phi[i * primes[j]] = phi[i] * primes[j]; //primes[j] divides i
                break;
            } else {
                phi[i * primes[j]] = phi[i] * phi[primes[j]]; //primes[j] does not divide i
            }
        }
    }
}
// }}}

// Number of divisors {{{
int cnt_divisors[MN + 11];  // call linear_sieve_divisors(n+1) to init
int cnt[MN + 11];           // power of smallest prime factor of i
void linear_sieve_divisors(int n) {  // init range [1, n-1]
    memset(is_composite, false, sizeof is_composite);
    primes.clear();

    cnt_divisors[1] = 1;
    for (int i = 2; i < n; ++i) {
        if (!is_composite[i]) {
            primes.push_back(i);
            cnt[i] = 1;
            cnt_divisors[i] = 2;
        }
        for (int j = 0; j < (int) primes.size() && i * primes[j] < n; ++j) {
            int ip = i * primes[j];
            is_composite[ip] = true;
            if (i % primes[j] == 0) {
                cnt[ip] = cnt[i] + 1;
                cnt_divisors[ip] = cnt_divisors[i] / (cnt[i] + 1) * (cnt[i] + 2);
            } else {
                cnt[ip] = 1;
                cnt_divisors[ip] = 2 * cnt_divisors[i];
            }
        }
    }
}
// }}}

}
#line 12 "Math/tests/cnt_divisors_stress.test.cpp"

void solve() {
    linear_sieve::linear_sieve_divisors(N + 1);
    for (int i = 1; i <= N; ++i) {
        assert(divisors[i] == cnt_divisors(i));
        assert(divisors[i] == linear_sieve::cnt_divisors[i]);
    }
    cout << "Hello World\n";
}
Back to top page