Submission #39391116


Source Code Expand

#include <bits/stdc++.h>

using i64 = long long;
template<class T>
constexpr T power(T a, i64 b) {
    T res = 1;
    for (; b; b /= 2, a *= a) {
        if (b % 2) {
            res *= a;
        }
    }
    return res;
}

template<int P>
struct MInt {
    int x;
    constexpr MInt() : x{} {}
    constexpr MInt(i64 x) : x{norm(x % P)} {}
    
    constexpr int norm(int x) const {
        if (x < 0) {
            x += P;
        }
        if (x >= P) {
            x -= P;
        }
        return x;
    }
    constexpr int val() const {
        return x;
    }
    explicit constexpr operator int() const {
        return x;
    }
    constexpr MInt operator-() const {
        MInt res;
        res.x = norm(P - x);
        return res;
    }
    constexpr MInt inv() const {
        assert(x != 0);
        return power(*this, P - 2);
    }
    constexpr MInt &operator*=(MInt rhs) {
        x = 1LL * x * rhs.x % P;
        return *this;
    }
    constexpr MInt &operator+=(MInt rhs) {
        x = norm(x + rhs.x);
        return *this;
    }
    constexpr MInt &operator-=(MInt rhs) {
        x = norm(x - rhs.x);
        return *this;
    }
    constexpr MInt &operator/=(MInt rhs) {
        return *this *= rhs.inv();
    }
    friend constexpr MInt operator*(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res *= rhs;
        return res;
    }
    friend constexpr MInt operator+(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res += rhs;
        return res;
    }
    friend constexpr MInt operator-(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res -= rhs;
        return res;
    }
    friend constexpr MInt operator/(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res /= rhs;
        return res;
    }
    friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {
        i64 v;
        is >> v;
        a = MInt(v);
        return is;
    }
    friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) {
        return os << a.val();
    }
    friend constexpr bool operator==(MInt lhs, MInt rhs) {
        return lhs.val() == rhs.val();
    }
    friend constexpr bool operator!=(MInt lhs, MInt rhs) {
        return lhs.val() != rhs.val();
    }
};

template<int V, int P>
constexpr MInt<P> CInv = MInt<P>(V).inv();

constexpr int P = 998244353;
using Z = MInt<P>;

std::unordered_map<int, Z> fMu;

constexpr int N = 1E7;
std::vector<int> minp, primes;
std::vector<Z> mu;

void sieve(int n) {
    minp.assign(n + 1, 0);
    mu.resize(n);
    primes.clear();
    
    mu[1] = 1;
    for (int i = 2; i <= n; i++) {
        if (minp[i] == 0) {
            mu[i] = -1;
            minp[i] = i;
            primes.push_back(i);
        }
        
        for (auto p : primes) {
            if (i * p > n) {
                break;
            }
            minp[i * p] = p;
            if (p == minp[i]) {
                break;
            }
            mu[i * p] = -mu[i];
        }
    }
    
    for (int i = 1; i <= n; i++) {
        mu[i] += mu[i - 1];
    }
}


Z sumMu(int n) {
    if (n <= N) {
        return mu[n];
    }
    if (fMu.count(n)) {
        return fMu[n];
    }
    if (n == 0) {
        return 0;
    }
    Z ans = 1;
    for (int l = 2, r; l <= n; l = r + 1) {
        r = n / (n / l);
        ans -= (r - l + 1) * sumMu(n / l);
    }
    return ans;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    sieve(N);
    
    int L, R;
    std::cin >> L >> R;
    L -= 1;
    
    Z ans = 0;
    for (int l = 1, r; l <= R; l = r + 1) {
        r = R / (R / l);
        if (l <= L) {
            r = std::min(r, L / (L / l));
        }
        
        ans += (power(Z(2), R / l - L / l) - 1) * (sumMu(r) - sumMu(l - 1));
    }
    
    std::cout << ans << "\n";
    
    return 0;
}

Submission Info

Submission Time
Task I - Count Setwise Coprime
User jiangly
Language C++ (GCC 9.2.1)
Score 400
Code Size 3996 Byte
Status AC
Exec Time 1165 ms
Memory 85464 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 4
AC × 30
Set Name Test Cases
Sample 00_sample_01, 00_sample_02, 00_sample_03, 00_sample_04
All 00_sample_01, 00_sample_02, 00_sample_03, 00_sample_04, 01_small_01, 01_small_02, 01_small_03, 01_small_04, 01_small_05, 02_random_01, 02_random_02, 02_random_03, 02_random_04, 03_big_01, 03_big_02, 03_big_03, 03_big_04, 03_big_05, 03_big_06, 04_handmade_01, 04_handmade_02, 04_handmade_03, 04_handmade_04, 04_handmade_05, 04_handmade_06, 04_handmade_07, 04_handmade_08, 04_handmade_09, 04_handmade_10, 04_handmade_11
Case Name Status Exec Time Memory
00_sample_01 AC 203 ms 85360 KiB
00_sample_02 AC 185 ms 85456 KiB
00_sample_03 AC 184 ms 85396 KiB
00_sample_04 AC 630 ms 85400 KiB
01_small_01 AC 189 ms 85400 KiB
01_small_02 AC 188 ms 85268 KiB
01_small_03 AC 186 ms 85276 KiB
01_small_04 AC 184 ms 85348 KiB
01_small_05 AC 185 ms 85336 KiB
02_random_01 AC 195 ms 85428 KiB
02_random_02 AC 198 ms 85464 KiB
02_random_03 AC 189 ms 85260 KiB
02_random_04 AC 191 ms 85352 KiB
03_big_01 AC 574 ms 85396 KiB
03_big_02 AC 632 ms 85272 KiB
03_big_03 AC 1107 ms 85392 KiB
03_big_04 AC 1046 ms 85352 KiB
03_big_05 AC 1116 ms 85332 KiB
03_big_06 AC 1165 ms 85424 KiB
04_handmade_01 AC 186 ms 85400 KiB
04_handmade_02 AC 185 ms 85284 KiB
04_handmade_03 AC 183 ms 85400 KiB
04_handmade_04 AC 622 ms 85364 KiB
04_handmade_05 AC 620 ms 85464 KiB
04_handmade_06 AC 621 ms 85364 KiB
04_handmade_07 AC 621 ms 85428 KiB
04_handmade_08 AC 1088 ms 85456 KiB
04_handmade_09 AC 1112 ms 85400 KiB
04_handmade_10 AC 1124 ms 85284 KiB
04_handmade_11 AC 1059 ms 85328 KiB