Consider a NNN lattice. One corner is at (0,0,0) and the opposite one is at (N,N,N). How many lattice points are visible from corner at (0,0,0) ? A point X is visible from point Y iff no other lattice point lies on the segment joining X and Y.
Input :
The first line contains the number of test cases T. The next T lines contain an interger N
Output :
Output T lines, one corresponding to each test case.
Sample Input :
3
1
2
5
Sample Output :
7
19
175
Constraints :
T <= 50
1 <= N <= 1000000
空间和平面上的点都可用莫比乌斯反演解决
懒得写公式了。。。参考博客:https://www.cnblogs.com/shenben/p/6748677.html
#include<bits/stdc++.h>
#define N 1000005
#define ll long long
using namespace std;
int p[N],check[N],tot;
int mu[N];
ll sum[N];
int T,n,m,d;
ll ans;
void init() //算出莫比乌斯函数和前缀和
{
mu[1]=1;
n=1e6;
for(int i=2;i<=n;i++)
{
if (!check[i])
{
p[++tot]=i;
mu[i]=-1;
}
for (int j=1;j<=tot && p[j]*i<=n;j++)
{
check[i*p[j]]=1;
if (i%p[j]==0)
{
mu[i*p[j]]=0;
break;
}
else mu[i*p[j]]=-mu[i];
}
}
for(int i=1;i<=n;i++)
sum[i]=(ll)mu[i]+sum[i-1];
}
ll calc(int n) //整除分块
{
ll ret=3;
for (int L=1,R;L<=n;L=R+1)
{
R=n/(n/L);
ret+=(1ll)*(sum[R]-sum[L-1])*((1ll)*(n/L)*(n/L)*(n/L)+3ll*(n/L)*(n/L));
}
return ret;
}
int main()
{
init();
scanf("%d",&T);
while (T--)
{
scanf("%d",&n);
ans=calc(n);
printf("%lld\n",ans);
}
return 0;
}