其他分享
首页 > 其他分享> > CF 1542C C. Strange Function

CF 1542C C. Strange Function

作者:互联网

https://codeforces.com/problemset/problem/1542/C

题意:
定义\(f(i)\)表示最小的不能整除i的数,求\(\sum_{i=1}^nf(i)\)

若\(f(i)=x\),说明\(1|i,2|i,3|i,……(x-1)|i,x \nmid i\),即\(lcm(1,2,3,……,x-1) | i,x \nmid i\)
所以\(f(i)>=x\)的\(i\)的个数等于 \(n/lcm(1,2,3,……,x-1)\)
\(f(i)=x\)的\(i\)的个数等于 \(n/lcm(1,2,3,……,x-1)\) - \(n/lcm(1,2,3,……,x-1,x)\)

#include<bits/stdc++.h>

using namespace std;

long long getgcd(long long a,long long b)
{
	return __gcd(a,b);
}

long long getlcm(long long a,long long b)
{
	return a/getgcd(a,b)*b;
}

int main()
{
	int T;
	long long n,lcm;
	const int mod=1e9+7;
	int ans;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%lld",&n);
		ans=0;
		lcm=1;
		for(int i=1;;++i)
		{
			lcm=getlcm(i,lcm);
			if(lcm>n) break;
			ans+=n/lcm%mod;
			ans%=mod;
		}
		ans=(ans+n)%mod;
		printf("%d\n",ans);
	}
}

标签:Function,getgcd,int,CF,long,Strange,ans,lcm,mod
来源: https://www.cnblogs.com/TheRoadToTheGold/p/15212393.html