QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#589534#8761. 另一个计数问题wtz2333AC ✓754ms21380kbC++202.6kb2024-09-25 18:18:572024-09-25 18:18:58

Judging History

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

  • [2024-09-25 18:18:58]
  • 评测
  • 测评结果:AC
  • 用时:754ms
  • 内存:21380kb
  • [2024-09-25 18:18:57]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;
using ll = long long;
const int mo = 998244353;
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 qp(int a,int b) {
    int ans = 1,base = a;
    while(b != 0) {
        if(b & 1) ans = 1ll * ans * base % mo;
        base = 1ll * base * base % mo;
        b >>= 1;
    }
    return ans;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n;
    cin >> n;
    ll s = sqrt(n);
    sieve(s);
    vector<ll> v;
    for(ll l = 1,r;l <= n;l = r + 1) {
        v.push_back(n / l);
        r = n / (n / l);
    }

    vector<ll> f1(v.size()),f2(v.size());
    auto getid = [&](ll x) -> ll {
        if(x <= s) return v.size() - x;
        else return n / x - 1;
    };
    ll inv2 = qp(2,mo - 2),inv6 = qp(6,mo - 2);
    for(int i = 0;i < v.size();i ++) {
        ll x = v[i];
        x %= mo;
        f1[i] = ((x + 1) * x % mo * inv2 % mo + mo - 1) % mo;// \sum_2^n i^k = \sum_1^n i^k - 1
        f2[i] = (x * (x + 1) % mo * (2 * x % mo + 1) % mo * inv6 % mo + mo - 1) % mo;
    }
    ll 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] -= 1ll * p * (f1[getid(v[i] / p)] - ps1 + mo) % mo;
            f1[i] += mo;f1[i] %= mo;
            f2[i] -= 1ll * p * p % mo * (f2[getid(v[i] / p)] - ps2 + mo) % mo;
            f2[i] += mo;f2[i] %= mo;
        }
        ps1 += p;
        ps1 %= mo;
        ps2 += 1ll * p * p % mo;
        ps2 %= mo;
    }
    // cerr << f1[getid(n)] << " " << f2[getid(n)] << endl;
    ll F1 = (f1[getid(n)] - f1[getid(n / 2)] + mo) % mo;
    ll F2 = (f2[getid(n)] - f2[getid(n / 2)] + mo) % mo;
    n %= mo;
    ll sum1 = (2 + n) % mo * (n - 1) % mo * inv2 % mo;
    ll sum2 = (n % mo * (n + 1) % mo * (2 * n % mo + 1) % mo * qp(6,mo - 2) % mo + mo - 1) % mo;




    // cerr << F1 << " " << F2 << endl;
    ll ans = (sum1 * F1 % mo - (F1 * F1 % mo - F2 + mo) % mo * inv2 % mo + mo - F2 + mo) % mo;
    // cerr << ans << endl;
    ll Ans = (sum1 * sum1 % mo - sum2 + mo) % mo * inv2 % mo;
    // cerr << Ans << endl;
    ans = (Ans - ans + mo) % mo;
    cout << ans << endl; 
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

4

output:

8

result:

ok 1 number(s): "8"

Test #2:

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

input:

5

output:

8

result:

ok 1 number(s): "8"

Test #3:

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

input:

6

output:

80

result:

ok 1 number(s): "80"

Test #4:

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

input:

7

output:

80

result:

ok 1 number(s): "80"

Test #5:

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

input:

8

output:

200

result:

ok 1 number(s): "200"

Test #6:

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

input:

9

output:

407

result:

ok 1 number(s): "407"

Test #7:

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

input:

10

output:

937

result:

ok 1 number(s): "937"

Test #8:

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

input:

79

output:

3224298

result:

ok 1 number(s): "3224298"

Test #9:

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

input:

123

output:

21077222

result:

ok 1 number(s): "21077222"

Test #10:

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

input:

158

output:

57411585

result:

ok 1 number(s): "57411585"

Test #11:

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

input:

285

output:

605750829

result:

ok 1 number(s): "605750829"

Test #12:

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

input:

355

output:

509863120

result:

ok 1 number(s): "509863120"

Test #13:

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

input:

484

output:

311440260

result:

ok 1 number(s): "311440260"

Test #14:

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

input:

520

output:

102191845

result:

ok 1 number(s): "102191845"

Test #15:

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

input:

706

output:

300787918

result:

ok 1 number(s): "300787918"

Test #16:

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

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: 0ms
memory: 3616kb

input:

76879

output:

716166793

result:

ok 1 number(s): "716166793"

Test #19:

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

input:

209295

output:

753032272

result:

ok 1 number(s): "753032272"

Test #20:

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

input:

220895

output:

874612082

result:

ok 1 number(s): "874612082"

Test #21:

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

input:

243390

output:

68635874

result:

ok 1 number(s): "68635874"

Test #22:

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

input:

414767

output:

862578797

result:

ok 1 number(s): "862578797"

Test #23:

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

input:

431662

output:

231728766

result:

ok 1 number(s): "231728766"

Test #24:

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

input:

521130

output:

106207351

result:

ok 1 number(s): "106207351"

Test #25:

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

input:

668419

output:

580625063

result:

ok 1 number(s): "580625063"

Test #26:

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

input:

700378

output:

790849562

result:

ok 1 number(s): "790849562"

Test #27:

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

input:

965876

output:

856082142

result:

ok 1 number(s): "856082142"

Test #28:

score: 0
Accepted
time: 26ms
memory: 5004kb

input:

998244350

output:

539142456

result:

ok 1 number(s): "539142456"

Test #29:

score: 0
Accepted
time: 26ms
memory: 4816kb

input:

998244351

output:

730264865

result:

ok 1 number(s): "730264865"

Test #30:

score: 0
Accepted
time: 26ms
memory: 4956kb

input:

998244352

output:

326703895

result:

ok 1 number(s): "326703895"

Test #31:

score: 0
Accepted
time: 30ms
memory: 4876kb

input:

998244353

output:

326703895

result:

ok 1 number(s): "326703895"

Test #32:

score: 0
Accepted
time: 27ms
memory: 5004kb

input:

998244354

output:

730264864

result:

ok 1 number(s): "730264864"

Test #33:

score: 0
Accepted
time: 30ms
memory: 4804kb

input:

998244355

output:

539142451

result:

ok 1 number(s): "539142451"

Test #34:

score: 0
Accepted
time: 31ms
memory: 4816kb

input:

998244356

output:

751581014

result:

ok 1 number(s): "751581014"

Test #35:

score: 0
Accepted
time: 51ms
memory: 5744kb

input:

2165916141

output:

216013547

result:

ok 1 number(s): "216013547"

Test #36:

score: 0
Accepted
time: 72ms
memory: 6344kb

input:

3550627266

output:

318019384

result:

ok 1 number(s): "318019384"

Test #37:

score: 0
Accepted
time: 164ms
memory: 8796kb

input:

11640239920

output:

137498099

result:

ok 1 number(s): "137498099"

Test #38:

score: 0
Accepted
time: 199ms
memory: 9732kb

input:

16191777349

output:

991399721

result:

ok 1 number(s): "991399721"

Test #39:

score: 0
Accepted
time: 325ms
memory: 13264kb

input:

31326230483

output:

99981147

result:

ok 1 number(s): "99981147"

Test #40:

score: 0
Accepted
time: 336ms
memory: 13452kb

input:

32810385543

output:

284259680

result:

ok 1 number(s): "284259680"

Test #41:

score: 0
Accepted
time: 375ms
memory: 13068kb

input:

37368395332

output:

511468046

result:

ok 1 number(s): "511468046"

Test #42:

score: 0
Accepted
time: 395ms
memory: 14336kb

input:

40002331093

output:

282851705

result:

ok 1 number(s): "282851705"

Test #43:

score: 0
Accepted
time: 652ms
memory: 19836kb

input:

82884464396

output:

767050832

result:

ok 1 number(s): "767050832"

Test #44:

score: 0
Accepted
time: 725ms
memory: 19276kb

input:

96506992785

output:

31413975

result:

ok 1 number(s): "31413975"

Test #45:

score: 0
Accepted
time: 747ms
memory: 20964kb

input:

99999999995

output:

456189842

result:

ok 1 number(s): "456189842"

Test #46:

score: 0
Accepted
time: 742ms
memory: 21380kb

input:

99999999996

output:

516138273

result:

ok 1 number(s): "516138273"

Test #47:

score: 0
Accepted
time: 754ms
memory: 20372kb

input:

99999999997

output:

136420410

result:

ok 1 number(s): "136420410"

Test #48:

score: 0
Accepted
time: 736ms
memory: 20708kb

input:

99999999998

output:

841974696

result:

ok 1 number(s): "841974696"

Test #49:

score: 0
Accepted
time: 745ms
memory: 20280kb

input:

99999999999

output:

164762165

result:

ok 1 number(s): "164762165"

Test #50:

score: 0
Accepted
time: 751ms
memory: 20812kb

input:

100000000000

output:

627965619

result:

ok 1 number(s): "627965619"