https://codeforces.com/contest/1542/problem/C
题目大意
定义 f ( i ) f(i) f(i) 为不能整除 i i i 的正整数, 例如 f ( 1 ) = 2 f(1) = 2 f(1)=2, f ( 2 ) = 3 f(2) = 3 f(2)=3,求 f ( 1 ) + f ( 2 ) + . . . + f ( n ) f(1)+f(2)+...+f(n) f(1)+f(2)+...+f(n)
题解
先求 1 1 1到 i − 1 i-1 i−1 的最小公倍数,最大数不超过 1 e 16 1e^{16} 1e16
ans = 2 * n % mod;
ans=(ans+n/pre[i]);
代码
#include<bits/stdc++.h>
using namespace std;
#define re register int
#define int long long
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<<3)+(x<<1)+(ch^48);ch=getchar();}
return f==1?x:-x;
}
const int maxn=1e16+10,modp=1e9+7;
int gcd(int x,int y)
{
return !y?x:gcd(y,x%y);
}
int Lcm[50];
signed main()
{
int tmp=1,x=1;
while(1)
{
Lcm[tmp]=x;
x=x*tmp/gcd(x,tmp);
if(x>maxn)break;
tmp++;
}
//cout << tmp<<endl;
//for(re i=1;i<=tmp;++i)cout<<Lcm[i]<<" ";
int T=read();
while(T--)
{
int n=read(),ans=2*n%modp;
for(re i=3;i<=tmp;++i)
ans=(ans+n/Lcm[i])%modp;
printf("%lld\n",ans);
}
return 0;
}
大佬的代码搬过来了,侵删!