Codeforces Round #729 (Div. 2) C. Strange Function(lcm,简单容斥)

Posted issue是fw

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #729 (Div. 2) C. Strange Function(lcm,简单容斥)相关的知识,希望对你有一定的参考价值。

LINK


i i i为不是数字 x x x因子中最小的数

那么 1 1 1 i − 1 i-1 i1都是 x x x的因子

换句话说 n n n拥有因子 s 1 = l c m ( 1 , 2 , . . . i − 1 ) s_1=lcm(1,2,...i-1) s1=lcm(1,2,...i1)

而且 n n n没有因子 s 2 = l c m ( 1 , 2 , . . . i ) s_2=lcm(1,2,...i) s2=lcm(1,2,...i)

容易知道 s 2 s_2 s2 s 1 s_1 s1的倍数,存在包含关系,所以只要容斥一下

n / s 1 − n / s 2 n/s_1-n/s_2 n/s1n/s2就是 f ( x ) = i f(x)=i f(x)=i的个数

这样我们暴力枚举 i i i计算即可,因为 l c m ( 1... i ) lcm(1...i) lcm(1...i)很快就会大于 n n n,就可以退出

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 1e9+7;
const int maxn = 3e5+10;
int a[maxn],t,n;
int gcd(int a,int b){ return b==0?a:gcd(b,a%b); }
int lcm(int a,int b){ return a/gcd(a,b)*b; }
signed main()
{
	cin >> t;
	while( t-- )
	{
		cin >> n;
		//只要是x的倍数,那么因子中都有1..i,那么看看是否存在i+1即可 
		int ans = 1, nxt = 1, res = 0;
		for(int i=1; ;i++)
		{
			ans = lcm( ans,i );
			if( ans>n )	break;
			nxt = lcm( ans,i+1 );
			res = ( res+(i+1)*( n/ans-n/nxt )%mod )%mod;
		}		
		cout << res << endl;
	}	
} 

以上是关于Codeforces Round #729 (Div. 2) C. Strange Function(lcm,简单容斥)的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #729 (Div. 2)

Codeforces Round #729 div.2 A-E题解

Codeforces Round #729 div.2 A-E题解

Codeforces Round #729 (Div. 2) C(数学)

Codeforces Round #729 (Div. 2) D. Priority Queue(简单dp)

Codeforces Round #729 (Div. 2) C. Strange Function