本文搬运自本人初中博客园博客,若图片加载不出来,可到原文查看:https://www.cnblogs.com/zhangtingxi/p/16529785.html

题目地址

题目

xx 分解质因数结果为 x=p1k1p2k2pnknx=p_1^{k_1}p_2^{k_2}\cdots p_n^{k_n},令f(x)=(k1+1)(k2+1)(kn+1)f(x)=(k_1+1)(k_2+1)\cdots (k_n+1),求 i=lrf(i)\sum_{i=l}^rf(i)998244353998\,244\,353 取模的结果。

image

思路

显然,f(x)f(x) 就是 xx 的因数个数。

S(n)=i=1nf(i)S(n)=\sum_{i=1}^nf(i),所以答案在求 S(r)S(l1)S(r)-S(l-1)

对于任意 i[1,n]i\in[1,n],它作为小于等于 nn 的数的约数个数为 ni\dfrac n i(向下取整),于是 S(n)=i=1nniS(n)=\sum_{i=1}^n \dfrac n i

观察上式,S(n)S(n) 显然可以通过数论分块在 O(n)O(\sqrt n) 的时间求出。

总时间复杂度 O(r)O(\sqrt r)

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Problem: P3935 Calculating
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P3935
// Memory Limit: 128 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
//#define M
#define mo 998244353
//#define N
int n, m, i, j, k, T;
int l, r;

int S(int n)
{
int l, r, ans=0;
for(l=1; l<=n; l=r+1)
{
r=n/(n/l);
ans+=(r-l+1)*(n/l);
ans%=mo;
}
return ans;
}

signed main()
{
// freopen("tiaoshi.in", "r", stdin);
// freopen("tiaoshi.out", "w", stdout);
l=read(); r=read();
printf("%lld", ((S(r)-S(l-1))%mo+mo)%mo);
return 0;
}

总结

我当时做这题时卡在没看出 f(x)f(x) 是在求 xx 的因数个数 这个**这也看不出

看出来后就是常见trick了