【Codeforces1542C】Strange Function(数论)
题目大意:
若\(i\)为能被\(1,2,......,x-2,x-1\)整除,不能被\(x\)整除,则\(f(i)=x\),计算\(\sum_{i=1}^{n}f(i)\)。
设\(g(x)\)为\(1~n\)中满足条件\(x\)的数的数量,\(k\)为使\(lcm(1,2,......,k)>n\)的最小值。
\(\sum_{i=1}^{n}f(i)\)
\(=2\times g(能被1整除,不能被2整除)+3\times g(能被1、2整除,不能被3整除)\)
\(+...+n\times g(能被1、2、……、k-1整除,不能被n整除)\)
\(=2\times (g(能被1整除)-g(能被1、2整除))+3\times (g(能被1、2整除)-g(能被1、2、3整除))\)
\(+...+k\times (g(能被1、2、……、k-1整除)-g(能被1、2、……、k整除))\)
\(=2\times g(能被1整除)+g(能被1、2整除)+......+\)
\(g(能被1、2、……、k-1整除)-k\times g(能被1、2、……k整除)\)
\(=2\times \lfloor\frac{n}{1}\rfloor+\lfloor\frac{n}{lcm(1,2)}\rfloor+\lfloor\frac{n}{lcm(1,2,3)}\rfloor+......+\lfloor\frac{n}{lcm(1,2,3,......k-1)}\rfloor-\lfloor\frac{n}{lcm(1,2,3,......,k-1,k)}\rfloor\)
\(=2\times n+\lfloor\frac{n}{lcm(1,2)}\rfloor+\lfloor\frac{n}{lcm(1,2,3)}\rfloor+......+\lfloor\frac{n}{lcm(1,2,3,......k-1)}\rfloor\)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
ll n;
ll lcm(ll x,ll y){
return x/__gcd(x,y)*y;
}
int main(){
int T;
cin >> T;
while(T--){
cin >> n;
ll ans=n%mod;
for(ll i=1,j=1;n/j;i++,j=lcm(j,i)){
ans=(ans+n/j)%mod;
}
cout << ans << endl;
}
return 0;
}

浙公网安备 33010602011771号