【P3935 Calculating】题解

题目地址

题目

x 分解质因数结果为 x=p1k1p2k2pnkn,令f(x)=(k1+1)(k2+1)(kn+1),求 i=lrf(i)998244353 取模的结果。

image

思路

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

S(n)=i=1nf(i),所以答案在求 S(r)S(l1)

对于任意 i[1,n],它作为小于等于 n 的数的约数个数为 ni(向下取整),于是 S(n)=i=1nni

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

总时间复杂度 O(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 @   zhangtingxi  阅读(30)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示