# 51Nod 1594

## Description

$F\left(n\right)=\sum _{i=1}^{n}\sum _{j=1}^{n}\varphi \left(\varphi \left(i\right),\varphi \left(j\right)\right)$$F(n)=\sum_{i=1}^n\sum_{j=1}^nϕ(ϕ(i),ϕ(j))$

$\varphi \left(\varphi i,\varphi j\right)$$ϕ(ϕi,ϕj)$表示i,j欧拉函数值的最大公约数的欧拉函数值.

## Input示例

1
1


## Output示例

1


## Solution

$Ans=\sum _{i=1}^{n}\sum _{j=1}^{n}\varphi \left(\varphi i\right),\varphi \left(j\right)\right)$$Ans=\sum_{i=1}^n\sum_{j=1}^nϕ(ϕi),ϕ(j))$

$Ans=\sum _{d=1}^{n}\varphi \left(d\right)\ast \sum _{i=1}^{n}\sum _{j=1}^{n}\left[gcd\left(\varphi \left(i\right),\varphi \left(j\right)=d\right]$$Ans=\sum_{d=1}^n\phi(d)*\sum_{i=1}^n\sum_{j=1}^n[gcd(\phi(i),\phi(j)=d]$
$g\left(d\right)=\sum _{i=1}^{n}\sum _{j=1}^{n}\left[d|gcd\left(\varphi \left(i\right),\varphi \left(j\right)\right)\right]$$g(d)=\sum_{i=1}^n\sum_{j=1}^n[d|gcd(\phi(i),\phi(j))]$
$f\left(d\right)=\sum _{i=1}^{n}\sum _{j=1}^{n}\left[gcd\left(\varphi \left(i\right),\varphi \left(j\right)\right)=d\right]$$f(d)=\sum_{i=1}^n\sum_{j=1}^n[gcd(\phi(i),\phi(j))=d]$

$g\left(d\right)=\sum _{d|k}f\left(k\right)$$g(d)=\sum_{d|k}f(k)$
$f\left(n\right)=\sum _{n|d}\mu \left(\frac{d}{n}\right)\ast g\left(d\right)$$f(n)=\sum_{n|d}\mu(\frac{d}{n})*g(d)$
$Ans=\sum _{d=1}^{n}\varphi \left(d\right)\ast f\left(d\right)$$Ans=\sum_{d=1}^n\phi(d)*f(d)$
f(d)可以在O（nln(n)）的时间内求出

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;

#define N 2000020
#define M 2000000
#define LL long long

LL t[N],prime[N],phi[N],mu[N];
LL f[N],g[N];
int ti,n;

void pre()
{
phi[1]=mu[1]=1;
memset(t,0,sizeof(t));
memset(prime,0,sizeof(prime));
for (int i=2;i<=M;i++)
{
if (t[i]==0)
{
prime[++prime[0]]=i;
mu[i]=-1; phi[i]=i-1;
}
for (int j=1;j<=prime[0];j++)
{
int k=i*prime[j];
if (k>M) break;
t[k]=1;
if (i%prime[j]==0)
{
mu[k]=0;
phi[k]=phi[i]*prime[j];
break;
}
mu[k]=-mu[i];
phi[k]=phi[i]*phi[prime[j]];
}
}
}

LL solve(int n)
{
memset(t,0,sizeof(t));
memset(g,0,sizeof(g));
memset(f,0,sizeof(f));
for (int i=1;i<=n;i++)
t[phi[i]]++;
for (int d=1;d<=n;d++)
for (int i=1;i<=n/d;i++)
g[d]+=t[i*d];
for (int i=1;i<=n;i++)
g[i]=g[i]*g[i];
for (int d=1;d<=n;d++)
for (int i=1;i<=n/d;i++)
f[d]+=mu[i]*g[i*d];
//for (int i=1;i<=n;i++)
//  printf("%d %d %d %d %d\n",i,phi[i],mu[i],g[i],f[i]);
LL ans=0;
for (int i=1;i<=n;i++)
ans+=phi[i]*f[i];
return ans;
}

int main()
{
pre();
scanf("%d",&ti);
for (int ii=1;ii<=ti;ii++)
{
scanf("%d",&n);
printf("%lld\n",solve(n));
}
return 0;
}