【P3935 Calculating】题解

题目地址

题目

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

image

思路

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

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

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

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

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

Code

// 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)\) 是在求 \(x\) 的因数个数 这个**这也看不出

看出来后就是常见trick了

posted @ 2022-07-28 18:35  zhangtingxi  阅读(28)  评论(0编辑  收藏  举报