Consider a N*N*N 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
本人蒟蒻,如有错误,还望指正
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
一道裸的莫比乌斯反演,一个点 (x,y,z) 与原点连接的线段上没有其他的点的条件是 gcd(x,y,z)==1,然后对于x y z都不为0 ,其中有一个是0,有两个是0用莫比乌斯反演即可
注:f(d)表示gcd(x,y,z)=d的有多少
F(d)表示gcd(x,y,z)是d的倍数的有多少,显然 F(d)=(n/d)^3+3*(n/d)^2+3*(n/d); (分别表示x y z都不为0 ,其中有一个是0,有两个是0)
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
const LL MOD=1e9+7;
#define fi first
#define se second
const int MAXN=1000000+23;
LL prime[MAXN],total,miu[MAXN];
bool isprime[MAXN];
void make()
{
int m=MAXN-3;
memset(isprime,true,sizeof(isprime));
total=0;
isprime[0]=isprime[1]=false;
miu[1]=1;
for(int i=2;i<=m;i++)
{
if(isprime[i]){prime[++total]=i;miu[i]=-1;}
for(int j=1;j<=total&&prime[j]*i<=m;j++)
{
isprime[i*prime[j]]=false;
if(i%prime[j])
{
miu[i*prime[j]]=-miu[i];
}
else
{
miu[i*prime[j]]=0;
break;
}
}
}
miu[0]=0;
for(int i=1;i<=m;i++)miu[i]+=miu[i-1];
}
LL n;
int main()
{
make();
int T;
scanf("%d",&T);
while(T--)
{
scanf("%lld",&n);
LL ans=0;
for(LL i=1,last=0;i<=n;i=last+1)
{
last=n/(n/i);//块端点
LL z=n/i;
ans+=(miu[last]-miu[i-1])*(z*z*z+3*z*z+3*z);
}
//for(int i=1;i<=n;i++){int z=n/i;ans+=(miu[i])*(z*z*z+3*z*z+3*z);}
printf("%lld\n",ans);
}
return 0;
}
本人蒟蒻,如有错误,还望指正