链接
http://www.lydsy.com/JudgeOnline/problem.php?id=4804
题解
网上的题解都好麻烦啊还用莫比乌斯反演,哪用得着啊。
∑i=1n∑j=1nϕ(gcd(i,j)) =∑d=1nϕ(i)∑i=1⌊Nd⌋∑j=1⌊Nd⌋[gcd(i,j)=1]
令 s(x)=∑xi=1ϕ(i) ,原式
=∑d=1nϕ(i)(s(⌊ni⌋)×2−1)
时间复杂度 O(N+TN−−√)
代码
//欧拉函数
#include <cstdio>
#include <algorithm>
#define maxn 10000001
#define ll long long
using namespace std;
bool mark[maxn];
int prime[maxn/10];
ll phi[maxn], s[maxn];
void init()
{
ll i, j;
phi[1]=1;
for(i=2;i<maxn;i++)
{
if(!mark[i])prime[++prime[0]]=i, phi[i]=i-1;
for(j=1;j<=prime[0] and i*prime[j]<maxn;j++)
{
mark[i*prime[j]]=1;
if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}
phi[i*prime[j]]=phi[i]*phi[prime[j]];
}
}
for(i=1;i<maxn;i++)s[i]=s[i-1]+phi[i];
}
void work(ll N)
{
ll i, last, ans=0;
for(i=1;i<=N;i=last+1)
{
last=N/(N/i);
ans+=(s[last]-s[i-1])*((s[N/i]<<1)-1);
}
printf("%lld\n",ans);
}
int main()
{
ll T, N;
init();
for(scanf("%lld",&T);T;T--)
{
scanf("%lld",&N);
work(N);
}
return 0;
}