QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#448151#8761. 另一个计数问题propaneAC ✓921ms39380kbC++202.8kb2024-06-19 12:40:092024-06-19 12:40:09

Judging History

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

  • [2024-06-19 12:40:09]
  • 评测
  • 测评结果:AC
  • 用时:921ms
  • 内存:39380kb
  • [2024-06-19 12:40:09]
  • 提交

answer

#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long LL;
const int maxn = 1e6 + 5, mod = 998244353;
bool isPrime[maxn];
int cnt;
int id1[maxn], id2[maxn];
LL primes[maxn], w[maxn], g1[maxn], sum1[maxn], g2[maxn], sum2[maxn];
LL n, sq;

void init(){
    for(int i = 2; i <= sq; i++){
        if (!isPrime[i]) primes[++cnt] = i;
        for(int j = 1; i * primes[j] <= sq; j++){
            isPrime[i * primes[j]] = 1;
            if (i % primes[j] == 0) break;
        }
    }
    for(int i = 1; i <= cnt; i++){
        sum1[i] = (sum1[i - 1] + primes[i]) % mod;
        sum2[i] = (sum2[i - 1] + primes[i] * primes[i]) % mod;
    }
}

inline LL f1(LL x){
    x %= mod;
    return x * (x + 1) / 2 % mod;
}

inline LL f2(LL x){
    x %= mod;
    return x * (x + 1) % mod * (2 * x + 1) % mod * 166374059 % mod;
}

inline LL f3(LL x){
    return f1(x) * f1(x) % mod;
}

pair<LL, LL> prime_sum(LL t){

    auto get = [&](LL x){
        return x <= sq ? id1[x] : id2[t / x];
    };
    memset(g1, 0, sizeof g1);
    int m = 0;
    for(LL l = 1, r; l <= t; l = r + 1){
        // 离散化
        r = t / (t / l), w[++m] = t / l;
        // 修改4: 初始化g为多项式的前缀和,需要排除f(1),
        // 注意多项式前的系数必须为1,否则就不是完全积性函数.比如f(x) = k,必须转化为f(x) = 1.
        g1[m] = f1(w[m]) - 1, g2[m] = f2(w[m]) - 1;
        if (w[m] <= sq) id1[w[m]] = m;
        else id2[t / w[m]] = m;
    }
    for(int i = 1; i <= cnt; i++){
        for(int j = 1; j <= m && primes[i] * primes[i] <= w[j]; j++){
            // 修改5: g[j - 1] -> g[j]
            g1[j] = (g1[j] - primes[i] * (g1[get(w[j] / primes[i])] - sum1[i - 1]) % mod + mod) % mod;
            g2[j] = (g2[j] - primes[i] * primes[i] % mod * (g2[get(w[j] / primes[i])] - sum2[i - 1]) % mod + mod) % mod;
        }
    }
    return make_pair(g1[get(t)], g2[get(t)]);
}

int main(){
#ifdef LOCAL
    freopen("data.in","r",stdin);
    freopen("data.out","w",stdout);
#endif

    // 结论,如果两个数字(u,v),存在某个数字是大于n/2的质数,则没有贡献,否则有贡献
    // 所以求出所有大于n/2质数的和与平方和就可以计算了.
    scanf("%lld", &n);
    sq = sqrt(n);
    init();
    auto [s1, s2] = prime_sum(n);
    auto [s3, s4] = prime_sum(n / 2);
    LL sum = s1 - s3;
    if (sum < 0) sum += mod;
    LL sum2 = s2 - s4;
    if (sum2 < 0) sum2 += mod;
    LL t = (sum * (f1(n) - 1)) % mod;
    if (t < 0) t += mod;
    t -= (sum * sum - sum2) % mod * ((mod + 1) / 2) % mod;
    t = (t % mod + mod) % mod;
    t -= sum2;
    if (t < 0) t += mod;
    LL all = (f3(n) - f2(n)) * ((mod + 1) / 2) % mod - (f1(n) - 1);
    all = (all % mod + mod) % mod;
    LL ans = all - t;
    cout << (ans % mod + mod) % mod << '\n';

}

詳細信息

Test #1:

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

input:

4

output:

8

result:

ok 1 number(s): "8"

Test #2:

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

input:

5

output:

8

result:

ok 1 number(s): "8"

Test #3:

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

input:

6

output:

80

result:

ok 1 number(s): "80"

Test #4:

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

input:

7

output:

80

result:

ok 1 number(s): "80"

Test #5:

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

input:

8

output:

200

result:

ok 1 number(s): "200"

Test #6:

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

input:

9

output:

407

result:

ok 1 number(s): "407"

Test #7:

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

input:

10

output:

937

result:

ok 1 number(s): "937"

Test #8:

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

input:

79

output:

3224298

result:

ok 1 number(s): "3224298"

Test #9:

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

input:

123

output:

21077222

result:

ok 1 number(s): "21077222"

Test #10:

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

input:

158

output:

57411585

result:

ok 1 number(s): "57411585"

Test #11:

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

input:

285

output:

605750829

result:

ok 1 number(s): "605750829"

Test #12:

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

input:

355

output:

509863120

result:

ok 1 number(s): "509863120"

Test #13:

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

input:

484

output:

311440260

result:

ok 1 number(s): "311440260"

Test #14:

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

input:

520

output:

102191845

result:

ok 1 number(s): "102191845"

Test #15:

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

input:

706

output:

300787918

result:

ok 1 number(s): "300787918"

Test #16:

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

input:

747

output:

505062591

result:

ok 1 number(s): "505062591"

Test #17:

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

input:

784

output:

181810798

result:

ok 1 number(s): "181810798"

Test #18:

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

input:

76879

output:

716166793

result:

ok 1 number(s): "716166793"

Test #19:

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

input:

209295

output:

753032272

result:

ok 1 number(s): "753032272"

Test #20:

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

input:

220895

output:

874612082

result:

ok 1 number(s): "874612082"

Test #21:

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

input:

243390

output:

68635874

result:

ok 1 number(s): "68635874"

Test #22:

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

input:

414767

output:

862578797

result:

ok 1 number(s): "862578797"

Test #23:

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

input:

431662

output:

231728766

result:

ok 1 number(s): "231728766"

Test #24:

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

input:

521130

output:

106207351

result:

ok 1 number(s): "106207351"

Test #25:

score: 0
Accepted
time: 4ms
memory: 22152kb

input:

668419

output:

580625063

result:

ok 1 number(s): "580625063"

Test #26:

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

input:

700378

output:

790849562

result:

ok 1 number(s): "790849562"

Test #27:

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

input:

965876

output:

856082142

result:

ok 1 number(s): "856082142"

Test #28:

score: 0
Accepted
time: 34ms
memory: 22272kb

input:

998244350

output:

539142456

result:

ok 1 number(s): "539142456"

Test #29:

score: 0
Accepted
time: 35ms
memory: 22300kb

input:

998244351

output:

730264865

result:

ok 1 number(s): "730264865"

Test #30:

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

input:

998244352

output:

326703895

result:

ok 1 number(s): "326703895"

Test #31:

score: 0
Accepted
time: 35ms
memory: 22344kb

input:

998244353

output:

326703895

result:

ok 1 number(s): "326703895"

Test #32:

score: 0
Accepted
time: 35ms
memory: 22340kb

input:

998244354

output:

730264864

result:

ok 1 number(s): "730264864"

Test #33:

score: 0
Accepted
time: 35ms
memory: 26436kb

input:

998244355

output:

539142451

result:

ok 1 number(s): "539142451"

Test #34:

score: 0
Accepted
time: 40ms
memory: 22152kb

input:

998244356

output:

751581014

result:

ok 1 number(s): "751581014"

Test #35:

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

input:

2165916141

output:

216013547

result:

ok 1 number(s): "216013547"

Test #36:

score: 0
Accepted
time: 87ms
memory: 26440kb

input:

3550627266

output:

318019384

result:

ok 1 number(s): "318019384"

Test #37:

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

input:

11640239920

output:

137498099

result:

ok 1 number(s): "137498099"

Test #38:

score: 0
Accepted
time: 252ms
memory: 28568kb

input:

16191777349

output:

991399721

result:

ok 1 number(s): "991399721"

Test #39:

score: 0
Accepted
time: 401ms
memory: 30800kb

input:

31326230483

output:

99981147

result:

ok 1 number(s): "99981147"

Test #40:

score: 0
Accepted
time: 415ms
memory: 28608kb

input:

32810385543

output:

284259680

result:

ok 1 number(s): "284259680"

Test #41:

score: 0
Accepted
time: 454ms
memory: 34760kb

input:

37368395332

output:

511468046

result:

ok 1 number(s): "511468046"

Test #42:

score: 0
Accepted
time: 483ms
memory: 26788kb

input:

40002331093

output:

282851705

result:

ok 1 number(s): "282851705"

Test #43:

score: 0
Accepted
time: 815ms
memory: 32968kb

input:

82884464396

output:

767050832

result:

ok 1 number(s): "767050832"

Test #44:

score: 0
Accepted
time: 902ms
memory: 35264kb

input:

96506992785

output:

31413975

result:

ok 1 number(s): "31413975"

Test #45:

score: 0
Accepted
time: 919ms
memory: 35160kb

input:

99999999995

output:

456189842

result:

ok 1 number(s): "456189842"

Test #46:

score: 0
Accepted
time: 918ms
memory: 30992kb

input:

99999999996

output:

516138273

result:

ok 1 number(s): "516138273"

Test #47:

score: 0
Accepted
time: 918ms
memory: 33104kb

input:

99999999997

output:

136420410

result:

ok 1 number(s): "136420410"

Test #48:

score: 0
Accepted
time: 921ms
memory: 33196kb

input:

99999999998

output:

841974696

result:

ok 1 number(s): "841974696"

Test #49:

score: 0
Accepted
time: 913ms
memory: 39380kb

input:

99999999999

output:

164762165

result:

ok 1 number(s): "164762165"

Test #50:

score: 0
Accepted
time: 919ms
memory: 33096kb

input:

100000000000

output:

627965619

result:

ok 1 number(s): "627965619"