VLATTICE - Visible Lattice Points
【题目描述】
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
【输入描述】
The first line contains the number of test cases
T
.
The next
【输出描述】
Output
【Sample Input】
3
1
2
5
【Sample Output】
7
19
175
【Constraints】
T<=50
,
1<=N<=1000000
.
【Solution】
本题题意(让我来当一回翻译):
就是一个
n∗n∗n
的空间,一个人在
(0,0,0)
,问他能够看到几个点。
用莫比乌斯反演就很easy了。
【Code】
#include <iostream>
#include <cstdio>
#define LL long long
using namespace std;
int T;
LL n;
short miu[1000010];
int prime[1000010];
bool no_prime[1000010];
int main(){
scanf("%d",&T);
miu[1]=1;
for(LL i=2;i<=1000000;i++){
if(!no_prime[i]){
prime[++prime[0]]=i;
miu[i]=-1;
}
for(LL j=1;prime[j]*i<=1000000;j++){
no_prime[prime[j]*i]=true;
if(i%prime[j]==0){
miu[prime[j]*i]=0;
break;
}
miu[prime[j]*i]=-miu[i];
}
}
while(T--){
LL ans=3;
scanf("%lld",&n);
LL tmp=0;
while(1){
tmp++;
if(tmp>n)break;
ans+=miu[tmp]*((n/tmp)*(n/tmp)*(n/tmp+3));
}
printf("%lld\n",ans);
}
return 0;
}