QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#619454#8761. 另一个计数问题KomorebieAC ✓615ms44008kbC++172.8kb2024-10-07 14:15:012024-10-07 14:15:02

Judging History

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

  • [2024-10-07 14:15:02]
  • 评测
  • 测评结果:AC
  • 用时:615ms
  • 内存:44008kb
  • [2024-10-07 14:15:01]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define PII pair<int, int>
#define pi acos(-1.0)
constexpr int N = 1e6 + 10, mod = 998244353;
ll n, sq;
ll primes[N], st[N], sp1[N], sp2[N], cnt;
ll w[N], g1[N], g2[N], tot, id1[N], id2[N];

ll qpow(ll aa, int bb = mod - 2)
{
    ll res = 1;
    while (bb) {
        if (bb & 1) res = (res * aa) % mod;
        aa = (aa * aa) % mod;
        bb >>= 1;
    }
    return res;
}

ll inv2 = qpow(2), inv6 = qpow(6);

void sieve()
{
    for (int i = 2; i <= sq; i++) {
        if (!st[i]) {
            st[i] = primes[++cnt] = i;
            sp1[cnt] = (sp1[cnt - 1] + i) % mod;
            sp2[cnt] = (sp2[cnt - 1] + 1ll * i * i % mod) % mod;
        }
        for (int j = 1; j <= cnt && primes[j] * i < N; j++) {
            st[primes[j] * i] = primes[j];
            if (i % primes[j] == 0) break;
        }
    }
}

ll s1(ll x)
{
    x %= mod;
    return ((x * (x + 1) % mod) * inv2 % mod - 1 + mod) % mod;
}

ll s2(ll x)
{
    x %= mod;
    return (((x * (x + 1) % mod) * (2 * x + 1) % mod) * inv6 % mod - 1 + mod) % mod;
}

ll S(ll i, ll j)
{
    if (primes[j] >= i) return 0;

    ll p = i <= sq ? id1[i] : id2[n / i];
    ll ans = ((g2[p] - g1[p] + mod) % mod - (sp2[j] - sp1[j] + mod) % mod + mod) % mod;

    for (int k = j + 1; primes[k] * primes[k] <= i && k <= cnt; k++) {
        ll pe = primes[k];
        for (int e = 1; pe <= i; e++, pe = pe * primes[k]) {
            ll x = pe % mod;
            ans = (ans + x * (x - 1) % mod * ((S(i / pe, k) + (e > 1)) % mod) % mod) % mod;
        }
    }
    return ans;
}

int main()
{
    ios::sync_with_stdio(false), cin.tie(nullptr);
    cin >> n;
    sq = sqrt(n);
    sieve();
    for (ll l = 1, r; l <= n; l = r + 1) {  // tot 记录所有的整除分块
        r = min(n, n / (n / l));
        w[++tot] = n / l;
        g1[tot] = s1(w[tot]);
        g2[tot] = s2(w[tot]);
        if (w[tot] <= sq)
            id1[w[tot]] = tot;
        else
            id2[n / w[tot]] = tot;
    }

    for (int j = 1; j <= cnt; j++)  // g(n, j)
    {
        for (int i = 1; i <= tot && primes[j] * primes[j] <= w[i]; i++) {
            ll tmp = w[i] / primes[j];
            ll p = tmp <= sq ? id1[tmp] : id2[n / tmp];
            g1[i] = (g1[i] - primes[j] * (g1[p] - sp1[j - 1] + mod) % mod + mod) % mod;
            g2[i] = (g2[i] - (primes[j] * primes[j] % mod) * (g2[p] - sp2[j - 1] + mod) % mod + mod) % mod;
        }
    }

    ll res1 = (g1[1] - g1[2] + mod) % mod;
    ll res2 = (g2[1] - g2[2] + mod) % mod;
    ll sum = s1(n);
    ll ans = ((sum * sum % mod - s2(n) + mod) % mod) * inv2 % mod;
    ans = (ans - res1 * sum % mod + mod + res2) % mod;
    ans = (ans + ((res1 * res1 % mod - res2 + mod) % mod) * inv2 % mod) % mod;
    cout << ans << endl;

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

4

output:

8

result:

ok 1 number(s): "8"

Test #2:

score: 0
Accepted
time: 2ms
memory: 11788kb

input:

5

output:

8

result:

ok 1 number(s): "8"

Test #3:

score: 0
Accepted
time: 2ms
memory: 11924kb

input:

6

output:

80

result:

ok 1 number(s): "80"

Test #4:

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

input:

7

output:

80

result:

ok 1 number(s): "80"

Test #5:

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

input:

8

output:

200

result:

ok 1 number(s): "200"

Test #6:

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

input:

9

output:

407

result:

ok 1 number(s): "407"

Test #7:

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

input:

10

output:

937

result:

ok 1 number(s): "937"

Test #8:

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

input:

79

output:

3224298

result:

ok 1 number(s): "3224298"

Test #9:

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

input:

123

output:

21077222

result:

ok 1 number(s): "21077222"

Test #10:

score: 0
Accepted
time: 2ms
memory: 11812kb

input:

158

output:

57411585

result:

ok 1 number(s): "57411585"

Test #11:

score: 0
Accepted
time: 2ms
memory: 11784kb

input:

285

output:

605750829

result:

ok 1 number(s): "605750829"

Test #12:

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

input:

355

output:

509863120

result:

ok 1 number(s): "509863120"

Test #13:

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

input:

484

output:

311440260

result:

ok 1 number(s): "311440260"

Test #14:

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

input:

520

output:

102191845

result:

ok 1 number(s): "102191845"

Test #15:

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

input:

706

output:

300787918

result:

ok 1 number(s): "300787918"

Test #16:

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

input:

747

output:

505062591

result:

ok 1 number(s): "505062591"

Test #17:

score: 0
Accepted
time: 2ms
memory: 11852kb

input:

784

output:

181810798

result:

ok 1 number(s): "181810798"

Test #18:

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

input:

76879

output:

716166793

result:

ok 1 number(s): "716166793"

Test #19:

score: 0
Accepted
time: 2ms
memory: 14936kb

input:

209295

output:

753032272

result:

ok 1 number(s): "753032272"

Test #20:

score: 0
Accepted
time: 2ms
memory: 15004kb

input:

220895

output:

874612082

result:

ok 1 number(s): "874612082"

Test #21:

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

input:

243390

output:

68635874

result:

ok 1 number(s): "68635874"

Test #22:

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

input:

414767

output:

862578797

result:

ok 1 number(s): "862578797"

Test #23:

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

input:

431662

output:

231728766

result:

ok 1 number(s): "231728766"

Test #24:

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

input:

521130

output:

106207351

result:

ok 1 number(s): "106207351"

Test #25:

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

input:

668419

output:

580625063

result:

ok 1 number(s): "580625063"

Test #26:

score: 0
Accepted
time: 3ms
memory: 17468kb

input:

700378

output:

790849562

result:

ok 1 number(s): "790849562"

Test #27:

score: 0
Accepted
time: 3ms
memory: 19348kb

input:

965876

output:

856082142

result:

ok 1 number(s): "856082142"

Test #28:

score: 0
Accepted
time: 24ms
memory: 21364kb

input:

998244350

output:

539142456

result:

ok 1 number(s): "539142456"

Test #29:

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

input:

998244351

output:

730264865

result:

ok 1 number(s): "730264865"

Test #30:

score: 0
Accepted
time: 20ms
memory: 24520kb

input:

998244352

output:

326703895

result:

ok 1 number(s): "326703895"

Test #31:

score: 0
Accepted
time: 24ms
memory: 22072kb

input:

998244353

output:

326703895

result:

ok 1 number(s): "326703895"

Test #32:

score: 0
Accepted
time: 24ms
memory: 20504kb

input:

998244354

output:

730264864

result:

ok 1 number(s): "730264864"

Test #33:

score: 0
Accepted
time: 23ms
memory: 20564kb

input:

998244355

output:

539142451

result:

ok 1 number(s): "539142451"

Test #34:

score: 0
Accepted
time: 17ms
memory: 24480kb

input:

998244356

output:

751581014

result:

ok 1 number(s): "751581014"

Test #35:

score: 0
Accepted
time: 36ms
memory: 21044kb

input:

2165916141

output:

216013547

result:

ok 1 number(s): "216013547"

Test #36:

score: 0
Accepted
time: 49ms
memory: 23680kb

input:

3550627266

output:

318019384

result:

ok 1 number(s): "318019384"

Test #37:

score: 0
Accepted
time: 132ms
memory: 26300kb

input:

11640239920

output:

137498099

result:

ok 1 number(s): "137498099"

Test #38:

score: 0
Accepted
time: 167ms
memory: 29796kb

input:

16191777349

output:

991399721

result:

ok 1 number(s): "991399721"

Test #39:

score: 0
Accepted
time: 266ms
memory: 32504kb

input:

31326230483

output:

99981147

result:

ok 1 number(s): "99981147"

Test #40:

score: 0
Accepted
time: 270ms
memory: 34144kb

input:

32810385543

output:

284259680

result:

ok 1 number(s): "284259680"

Test #41:

score: 0
Accepted
time: 304ms
memory: 30308kb

input:

37368395332

output:

511468046

result:

ok 1 number(s): "511468046"

Test #42:

score: 0
Accepted
time: 318ms
memory: 33640kb

input:

40002331093

output:

282851705

result:

ok 1 number(s): "282851705"

Test #43:

score: 0
Accepted
time: 530ms
memory: 39368kb

input:

82884464396

output:

767050832

result:

ok 1 number(s): "767050832"

Test #44:

score: 0
Accepted
time: 591ms
memory: 43784kb

input:

96506992785

output:

31413975

result:

ok 1 number(s): "31413975"

Test #45:

score: 0
Accepted
time: 606ms
memory: 41304kb

input:

99999999995

output:

456189842

result:

ok 1 number(s): "456189842"

Test #46:

score: 0
Accepted
time: 607ms
memory: 44008kb

input:

99999999996

output:

516138273

result:

ok 1 number(s): "516138273"

Test #47:

score: 0
Accepted
time: 615ms
memory: 41192kb

input:

99999999997

output:

136420410

result:

ok 1 number(s): "136420410"

Test #48:

score: 0
Accepted
time: 606ms
memory: 40428kb

input:

99999999998

output:

841974696

result:

ok 1 number(s): "841974696"

Test #49:

score: 0
Accepted
time: 607ms
memory: 42248kb

input:

99999999999

output:

164762165

result:

ok 1 number(s): "164762165"

Test #50:

score: 0
Accepted
time: 613ms
memory: 42268kb

input:

100000000000

output:

627965619

result:

ok 1 number(s): "627965619"