Kuro-orzz's Library

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

View on GitHub

:warning: NumberTheory/Math/Divisors.h

Depends on

Code

#include "../../template.h"
#include "MillerRabin.h"
#include "Sieve.h"

using u128 = __uint128_t;

// sum of all divisor [1, n]
// https://usaco.guide/problems/cses-1082-sum-of-divisors/solution
ll sum_of_divisor(ll n) {
    ll res = 0, i = 1;
    while (i <= n) {
        ll l = i;          
        ll r = n / (n / i);
        ll val = n / l;
        ll csc = (u128)(r - l + 1) * (l + r) / 2 % MOD;
        res = (res + csc * val) % MOD;
        i = r + 1;
    }
    return res;
}

// count number of divisor up to n <= 1e18
// AC: https://codeforces.com/gym/100753 (Probblem F)
int count_divisor(ll n) {
    if (n == 1) return 1;
    vector<int> prime = listPrime(1, 1e6+5);
    int ans = 1;
    for (int p : prime) {
        if (1ll * p * p * p > n) break;
        int cnt = 0;
        while (n % p == 0) {
            n /= p;
            cnt++;
        }
        ans *= cnt + 1;
    }
    auto isSqrt = [&](ll n)->bool {
        ll c = sqrtl(n);
        return c * c == n;
    };
    if (n == 1) return ans;
    if (MillerRabin(n)) ans *= 2;
    else if (isSqrt(n)) ans *= 3;
    else ans *= 4;
    return ans;
}
#line 2 "template.h"

#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
#define MOD (ll)(1e9+7)
#define all(x) (x).begin(),(x).end()
#define unique(x) x.erase(unique(all(x)), x.end())
#define INF32 ((1ull<<31)-1)
#define INF64 ((1ull<<63)-1)
#define inf (ll)1e18

#define vi vector<int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second

const int mod = 998244353;

void solve();

int main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    // cin.exceptions(cin.failbit);
    // int t; cin >> t;
    // while(t--)
        solve();
    cerr << "\nTime run: " << 1000 * clock() / CLOCKS_PER_SEC << "ms" << '\n';
    return 0;
}
#line 2 "NumberTheory/Math/Binary_exponentiation.h"

using u128 = __uint128_t;

ll binMul(ll a, ll b, ll M) {
    a = a % M;
    ll res = 0;
    while (b) {
        if (b & 1) res = (res + a) % M;
        a = a * 2 % M;
        b /= 2;
    }
    return res;
}

ll binPow(ll a, ll b, ll M) {
    a %= M;
    ll res = 1;
    while (b) {
        if (b & 1) res = (u128)res * a % M;
        a = (u128)a * a % M;
        b /= 2;
    }
    return res;
}
#line 3 "NumberTheory/Math/MillerRabin.h"

bool test(ll a, ll n, ll k, ll m) {
    ll mod = binPow(a, m, n);
    if (mod == 1 || mod == n - 1) return true;
    for (int l = 1; l < k; l++) {
        mod = (u128)mod * mod % n;
        if (mod == n - 1) return true;
    }
    return false;
}

// Miller rabin
bool MillerRabin0(ll n) {
    if (n == 2) return true;
    if (n < 2 || n % 2 == 0) return false;
    ll k = 0, m = n - 1;
    while(m % 2 == 0) {
        m /= 2;
        k++;
    }
    for (int i = 0; i < 5; i++) {
        ll a = rand() % (n - 3) + 2;
        if (!test(a, n, k, m)) return false;
    }
    return true;
}

// Miller Rabin deterministic version
bool MillerRabin(ll n) {
    if (n <= 1) return false;
    ll k = 0, m = n-1;
    while (m % 2 == 0) {
        m /= 2;
        k++;
    }
    for (int a : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}) {
        if (n == a) return true;
        if (!test(a, n, k, m)) return false;
    }
    return true;
}
#line 2 "NumberTheory/Math/Sieve.h"

vector<int> sieve(int n) {
    vector<int> nt(n+1, 1);
    nt[0] = nt[1] = 0;
    for (int i = 2; i * i <= n; i++) {
        if (!nt[i]) continue;
        for (int j = i * i; j <= n; j += i)
            nt[j] = 0;
    }
    return nt;
}

vector<int> segmentSieve(int l, int r){
    vector<int> prime(r-l+1, 1);
    for(ll p = 2; p*p <= r; p++){
        ll lim = max(p*p, (l+p-1)/p*p);
        for(ll j = lim; j <= r; j += p)
            if (j-l >= 0) prime[j-l] = 0;
    }
    if (l == 0) prime[0] = 0;
    if (l == 0 && r > l) prime[1] = 0;
    if (l == 1) prime[1-l] = 0;
    return prime;
}

vector<int> listPrime(int l, int r) {
    vector<int> prime = segmentSieve(l, r);
    vector<int> listPi;
    for (int i = l; i <= r; i++) {
        if (prime[i-l]) listPi.push_back(i);
    }
    return listPi;
}
#line 4 "NumberTheory/Math/Divisors.h"

using u128 = __uint128_t;

// sum of all divisor [1, n]
// https://usaco.guide/problems/cses-1082-sum-of-divisors/solution
ll sum_of_divisor(ll n) {
    ll res = 0, i = 1;
    while (i <= n) {
        ll l = i;          
        ll r = n / (n / i);
        ll val = n / l;
        ll csc = (u128)(r - l + 1) * (l + r) / 2 % MOD;
        res = (res + csc * val) % MOD;
        i = r + 1;
    }
    return res;
}

// count number of divisor up to n <= 1e18
// AC: https://codeforces.com/gym/100753 (Probblem F)
int count_divisor(ll n) {
    if (n == 1) return 1;
    vector<int> prime = listPrime(1, 1e6+5);
    int ans = 1;
    for (int p : prime) {
        if (1ll * p * p * p > n) break;
        int cnt = 0;
        while (n % p == 0) {
            n /= p;
            cnt++;
        }
        ans *= cnt + 1;
    }
    auto isSqrt = [&](ll n)->bool {
        ll c = sqrtl(n);
        return c * c == n;
    };
    if (n == 1) return ans;
    if (MillerRabin(n)) ans *= 2;
    else if (isSqrt(n)) ans *= 3;
    else ans *= 4;
    return ans;
}
Back to top page