QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#447506#8761. 另一个计数问题ucup-team004#AC ✓1209ms21272kbC++204.7kb2024-06-18 15:24:312024-06-18 15:24:32

Judging History

你现在查看的是最新测评结果

  • [2024-06-18 15:24:32]
  • 评测
  • 测评结果:AC
  • 用时:1209ms
  • 内存:21272kb
  • [2024-06-18 15:24:31]
  • 提交

answer

#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;
}

constexpr i64 mul(i64 a, i64 b, i64 p) {
    i64 res = a * b - i64(1.L * a * b / p) * p;
    res %= p;
    if (res < 0) {
        res += p;
    }
    return res;
}

template<i64 P>
struct MInt {
    i64 x;
    constexpr MInt() : x {0} {}
    constexpr MInt(i64 x) : x {norm(x % getMod())} {}
    
    static i64 Mod;
    constexpr static i64 getMod() {
        if (P > 0) {
            return P;
        } else {
            return Mod;
        }
    }
    constexpr static void setMod(i64 Mod_) {
        Mod = Mod_;
    }
    constexpr i64 norm(i64 x) const {
        if (x < 0) {
            x += getMod();
        }
        if (x >= getMod()) {
            x -= getMod();
        }
        return x;
    }
    constexpr i64 val() const {
        return x;
    }
    constexpr MInt operator-() const {
        MInt res;
        res.x = norm(getMod() - x);
        return res;
    }
    constexpr MInt inv() const {
        return power(*this, getMod() - 2);
    }
    constexpr MInt &operator*=(MInt rhs) & {
        if (getMod() < (1ULL << 31)) {
            x = x * rhs.x % int(getMod());
        } else {
            x = mul(x, rhs.x, getMod());
        }
        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();
    }
    friend constexpr bool operator<(MInt lhs, MInt rhs) {
        return lhs.val() < rhs.val();
    }
};

template<>
i64 MInt<0>::Mod = 998244353;

constexpr int P = 998244353;
using Z = MInt<P>;
std::vector<int> minp, primes;

void sieve(int n) {
    minp.assign(n + 1, 0);
    primes.clear();
    
    for (int i = 2; i <= n; i++) {
        if (minp[i] == 0) {
            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;
            }
        }
    }
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    i64 n;
    std::cin >> n;
    
    Z S1 = Z(n + 2) * (n - 1) / 2;
    Z S2 = Z(n) * (n + 1) * (2 * n + 1) / 6 - 1;
    
    std::vector<i64> v;
    for (i64 l = 1, r; l <= n; l = r + 1) {
        v.push_back(n / l);
        r = n / (n / l);
    }
    const i64 sqrtn = std::sqrt(n);
    sieve(sqrtn);
    
    std::vector<Z> f1(v.size()), f2(v.size());
    auto get = [&](auto &f, i64 x) -> Z & {
        if (x <= sqrtn) {
            return f[v.size() - x];
        } else {
            return f[n / x - 1];
        }
    };
    for (int i = 0; i < v.size(); i++) {
        Z x = v[i];
        f1[i] = x * (x + 1) / 2 - 1;
        f2[i] = x * (x + 1) * (2 * x + 1) / 6 - 1;
    }
    
    Z ps1 = 0, ps2 = 0;
    for (auto p : primes) {
        for (int i = 0; i < v.size(); i++) {
            if (1LL * p * p > v[i]) {
                break;
            }
            f1[i] -= Z(p) * (get(f1, v[i] / p) - ps1);
            f2[i] -= Z(p) * p * (get(f2, v[i] / p) - ps2);
        }
        ps1 += Z(p);
        ps2 += Z(p) * p;
    }
    
    S1 -= f1[0] - f1[1];
    S2 -= f2[0] - f2[1];
    Z ans = (S1 * S1 - S2) / 2;
    std::cout << ans << "\n";
    
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3776kb

input:

4

output:

8

result:

ok 1 number(s): "8"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3536kb

input:

5

output:

8

result:

ok 1 number(s): "8"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3712kb

input:

6

output:

80

result:

ok 1 number(s): "80"

Test #4:

score: 0
Accepted
time: 0ms
memory: 3844kb

input:

7

output:

80

result:

ok 1 number(s): "80"

Test #5:

score: 0
Accepted
time: 0ms
memory: 3640kb

input:

8

output:

200

result:

ok 1 number(s): "200"

Test #6:

score: 0
Accepted
time: 0ms
memory: 3584kb

input:

9

output:

407

result:

ok 1 number(s): "407"

Test #7:

score: 0
Accepted
time: 1ms
memory: 3584kb

input:

10

output:

937

result:

ok 1 number(s): "937"

Test #8:

score: 0
Accepted
time: 0ms
memory: 3644kb

input:

79

output:

3224298

result:

ok 1 number(s): "3224298"

Test #9:

score: 0
Accepted
time: 0ms
memory: 3532kb

input:

123

output:

21077222

result:

ok 1 number(s): "21077222"

Test #10:

score: 0
Accepted
time: 0ms
memory: 3592kb

input:

158

output:

57411585

result:

ok 1 number(s): "57411585"

Test #11:

score: 0
Accepted
time: 0ms
memory: 3792kb

input:

285

output:

605750829

result:

ok 1 number(s): "605750829"

Test #12:

score: 0
Accepted
time: 0ms
memory: 3792kb

input:

355

output:

509863120

result:

ok 1 number(s): "509863120"

Test #13:

score: 0
Accepted
time: 0ms
memory: 3560kb

input:

484

output:

311440260

result:

ok 1 number(s): "311440260"

Test #14:

score: 0
Accepted
time: 0ms
memory: 3792kb

input:

520

output:

102191845

result:

ok 1 number(s): "102191845"

Test #15:

score: 0
Accepted
time: 0ms
memory: 3776kb

input:

706

output:

300787918

result:

ok 1 number(s): "300787918"

Test #16:

score: 0
Accepted
time: 0ms
memory: 3560kb

input:

747

output:

505062591

result:

ok 1 number(s): "505062591"

Test #17:

score: 0
Accepted
time: 0ms
memory: 3640kb

input:

784

output:

181810798

result:

ok 1 number(s): "181810798"

Test #18:

score: 0
Accepted
time: 1ms
memory: 3556kb

input:

76879

output:

716166793

result:

ok 1 number(s): "716166793"

Test #19:

score: 0
Accepted
time: 0ms
memory: 3800kb

input:

209295

output:

753032272

result:

ok 1 number(s): "753032272"

Test #20:

score: 0
Accepted
time: 1ms
memory: 3740kb

input:

220895

output:

874612082

result:

ok 1 number(s): "874612082"

Test #21:

score: 0
Accepted
time: 1ms
memory: 3616kb

input:

243390

output:

68635874

result:

ok 1 number(s): "68635874"

Test #22:

score: 0
Accepted
time: 1ms
memory: 3660kb

input:

414767

output:

862578797

result:

ok 1 number(s): "862578797"

Test #23:

score: 0
Accepted
time: 1ms
memory: 3540kb

input:

431662

output:

231728766

result:

ok 1 number(s): "231728766"

Test #24:

score: 0
Accepted
time: 0ms
memory: 3832kb

input:

521130

output:

106207351

result:

ok 1 number(s): "106207351"

Test #25:

score: 0
Accepted
time: 1ms
memory: 3588kb

input:

668419

output:

580625063

result:

ok 1 number(s): "580625063"

Test #26:

score: 0
Accepted
time: 1ms
memory: 3696kb

input:

700378

output:

790849562

result:

ok 1 number(s): "790849562"

Test #27:

score: 0
Accepted
time: 1ms
memory: 3600kb

input:

965876

output:

856082142

result:

ok 1 number(s): "856082142"

Test #28:

score: 0
Accepted
time: 57ms
memory: 4628kb

input:

998244350

output:

539142456

result:

ok 1 number(s): "539142456"

Test #29:

score: 0
Accepted
time: 53ms
memory: 4832kb

input:

998244351

output:

730264865

result:

ok 1 number(s): "730264865"

Test #30:

score: 0
Accepted
time: 53ms
memory: 4812kb

input:

998244352

output:

326703895

result:

ok 1 number(s): "326703895"

Test #31:

score: 0
Accepted
time: 56ms
memory: 4704kb

input:

998244353

output:

326703895

result:

ok 1 number(s): "326703895"

Test #32:

score: 0
Accepted
time: 53ms
memory: 4728kb

input:

998244354

output:

730264864

result:

ok 1 number(s): "730264864"

Test #33:

score: 0
Accepted
time: 57ms
memory: 4856kb

input:

998244355

output:

539142451

result:

ok 1 number(s): "539142451"

Test #34:

score: 0
Accepted
time: 57ms
memory: 4852kb

input:

998244356

output:

751581014

result:

ok 1 number(s): "751581014"

Test #35:

score: 0
Accepted
time: 94ms
memory: 5416kb

input:

2165916141

output:

216013547

result:

ok 1 number(s): "216013547"

Test #36:

score: 0
Accepted
time: 126ms
memory: 6432kb

input:

3550627266

output:

318019384

result:

ok 1 number(s): "318019384"

Test #37:

score: 0
Accepted
time: 283ms
memory: 8656kb

input:

11640239920

output:

137498099

result:

ok 1 number(s): "137498099"

Test #38:

score: 0
Accepted
time: 348ms
memory: 9640kb

input:

16191777349

output:

991399721

result:

ok 1 number(s): "991399721"

Test #39:

score: 0
Accepted
time: 541ms
memory: 12636kb

input:

31326230483

output:

99981147

result:

ok 1 number(s): "99981147"

Test #40:

score: 0
Accepted
time: 562ms
memory: 12760kb

input:

32810385543

output:

284259680

result:

ok 1 number(s): "284259680"

Test #41:

score: 0
Accepted
time: 618ms
memory: 13092kb

input:

37368395332

output:

511468046

result:

ok 1 number(s): "511468046"

Test #42:

score: 0
Accepted
time: 646ms
memory: 13552kb

input:

40002331093

output:

282851705

result:

ok 1 number(s): "282851705"

Test #43:

score: 0
Accepted
time: 1066ms
memory: 18384kb

input:

82884464396

output:

767050832

result:

ok 1 number(s): "767050832"

Test #44:

score: 0
Accepted
time: 1173ms
memory: 20456kb

input:

96506992785

output:

31413975

result:

ok 1 number(s): "31413975"

Test #45:

score: 0
Accepted
time: 1201ms
memory: 21272kb

input:

99999999995

output:

456189842

result:

ok 1 number(s): "456189842"

Test #46:

score: 0
Accepted
time: 1202ms
memory: 20252kb

input:

99999999996

output:

516138273

result:

ok 1 number(s): "516138273"

Test #47:

score: 0
Accepted
time: 1205ms
memory: 20212kb

input:

99999999997

output:

136420410

result:

ok 1 number(s): "136420410"

Test #48:

score: 0
Accepted
time: 1198ms
memory: 19768kb

input:

99999999998

output:

841974696

result:

ok 1 number(s): "841974696"

Test #49:

score: 0
Accepted
time: 1194ms
memory: 20712kb

input:

99999999999

output:

164762165

result:

ok 1 number(s): "164762165"

Test #50:

score: 0
Accepted
time: 1209ms
memory: 21236kb

input:

100000000000

output:

627965619

result:

ok 1 number(s): "627965619"