跳到正文

题解

题解:UVA1730 Sum of MSLCM

RainRecall约 1 分钟
原文
文章信息与版本

文章信息

发布
2024年7月18日
收录
2025年12月12日
更新
2026年8月25日
篇幅
约 1 分钟
题目
UVA1730

版本

  1. v1当前

容易发现这个“最大的满足最小公倍数为 nn 的数集”就是 nn 的因数集,所以题目中的式子就是:

∑i=2n∑d∣nd\sum_{i=2}^n\sum _{d|n}d =(∑i=1n∑d∣nd)−1=(\sum_{i=1}^n\sum_{d|n}d)-1 =(∑d=1nd∑d∣i1)−1=(\sum_{d=1}^nd\sum_{d|i}1)-1 =(∑d=1nd×⌊nd⌋)−1=(\sum_{d=1}^nd\times \lfloor\frac{n}d\rfloor)-1

然后整除分块就做完了,时间复杂度 O(Tn)O(T\sqrt n)。

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,ans;
signed main(){
	while(cin>>n){
		if(!n)break;ans=0;
		for(int l=1,r;l<=n;l=r+1){
			r=n/(n/l);ans+=(l+r)*(r-l+1)/2*(n/l);
		}
		cout<<ans-1<<'\n';
	}
	return 0;
}
原文