链接
http://acm.hdu.edu.cn/showproblem.php?pid=5942
题解
Claris已经写的很好了:
https://www.cnblogs.com/clrs97/p/6012285.html
做出这题的关键就在于这个结论:
μ
2
(
n
)
=
∑
d
2
∣
n
μ
(
d
)
\mu^2(n)=\sum_{d^2|n}\mu(d)
μ2(n)=d2∣n∑μ(d)
代码
#include <bits/stdc++.h>
#define mod 1000000007ll
#define maxn 1000010
#define cl(x) memset(x,0,sizeof(x))
using namespace std;
typedef long long ll;
struct EasyMath
{
int prime[maxn], mu[maxn];
bool mark[maxn];
void shai(int N)
{
ll i, j;
for(i=2;i<=N;i++)mark[i]=false;
*prime=0;
mu[1]=1;
for(i=2;i<=N;i++)
{
if(!mark[i])prime[++*prime]=i, mu[i]=-1;
for(j=1;j<=*prime and i*prime[j]<=N;j++)
{
mark[i*prime[j]]=true;
if(i%prime[j]==0)break;
mu[i*prime[j]]=-mu[i];
}
}
}
}em;
ll s[10000000];
ll S(ll n)
{
if(n<10000000 and s[n])return s[n];
ll i, j, ans=0;
for(i=1;i<=n;i=j+1)
{
j=n/(n/i);
ans+=(j-i+1)*(n/i);
}
ans%=mod;
if(n<10000000)s[n]=ans;
return ans;
}
int main()
{
ll i, j, k, T, n, kase, ans;
em.shai(1000000);
cin>>T;
for(kase=1;kase<=T;kase++)
{
cin>>n;
ans=0;
for(i=1;i*i<=n;i++)
{
if(em.mu[i])(ans+=em.mu[i]*S(n/(i*i)))%=mod;
}
cout<<"Case #"<<kase<<": "<<(ans+mod)%mod<<endl;
}
return 0;
}