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

题目链接

题目大意:

定义一个函数f(i)表示不是i的因数的最小正整数值
例:
𝑓(1)=2 𝑓(2)=3 𝑓(3)=2 𝑓(4)=3
给你一个正整数n让你求 𝑓(1)+𝑓(2)+⋯+𝑓(𝑛) modulo 109+7
1<=n<=1e16

解题思路:

看数据n的范围这么大,可以判断出这是一道算贡献的题目。
通过分析我们知道,如果f(x) = i,那么x的因子一定包含1,2,…,i-1的最小公倍数,通过打表可知,i的最大值是44,使用我们只需要算出i从2到44的贡献即可。
通过上述分析,对于i贡献了多少个数,这些数的特点是,这些数的因子包含1,2,…,i-1的最小公倍数且不包含i这个因子,因子包含1,2,…,i-1的最小公倍数的数的个数为n/lcm(1,2,…,i-1),这些数中部包含因子i的个数为n/lcm(1,2,…,i),通过简单的容斥,i的贡献为上述两数之差再乘以i即可。

#include <bits/stdc++.h>
#define ll long long
#define qc ios::sync_with_stdio(false); cin.tie(0);cout.tie(0)
#define fi first
#define se second
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define pb push_back
using namespace std;
const int MAXN = 2e5 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1e9 + 7;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
    while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
    return x*f;
}
const ll N = 1e16;
ll gcd(ll a, ll b){
	return b ? gcd(b, a%b) : a;
}

void solve(){
	ll n;
	cin >> n;
	ll ans = 0;
	ll g = 1;
	ll mul = 1;
	int cnt ;
	for(ll i = 2; i <= 44 && g <= n; i++){
		ans += (n / g - n / (g*i/gcd(g, i))) * i % mod;
		ans %= mod;
		g = g * i / gcd(g, i);
	}
	cout << ans << endl;
}

int main()
{
	#ifdef ONLINE_JUDGE
    #else
       freopen("in.txt", "r", stdin);
       freopen("out.txt", "w", stdout);
    #endif

	qc;
    int T;
    cin >> T;
    // T = 1;
    while(T--){
        solve();
    }
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值