Description
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
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
1 <= N <= 1000000
题意:1<=X,Y,Z<=n,求GCD(X,Y,Z)=1 的(X,Y,Z)组数,由莫比乌斯反演可得,结果为∑mu[i]*(n/i)*(n/i)*(n/i)即可,
但是,这个没有考虑X,Y,Z轴上的三个点,以及三个二维平面上的点,后者把上面的一个 (n/i) 改成 (n/i+3) 即可。
#include <iostream>
#include <cstdio>
#include <map>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
#define LL long long
#define maxn 1000100
bool v[maxn];
int prime[maxn],mu[maxn],sum[maxn];
void Moblus()
{
memset(v,false,sizeof(v));
mu[1]=1;
sum[1]=1;
sum[0]=0;
int tot=0;
for(int i=2;i<=maxn;i++)
{
if(!v[i]){
prime[tot++]=i;
mu[i]=-1;
}
for(int j=0;j<tot;j++){
if(i*prime[j]>maxn) break;
v[i*prime[j]]=true;
if(i%prime[j]==0){
mu[i*prime[j]]=0;
break;
}else{
mu[i*prime[j]]=-mu[i];
}
}
sum[i]=sum[i-1]+mu[i];
}
}
int main()
{
int t,n;
Moblus();
scanf("%d",&t);
while(t--){
scanf("%d",&n);
LL ans=3;
for(int i=1,last;i<=n;i=last+1){
last=n/(n/i);
ans+=(LL)(sum[last]-sum[i-1])*(n/i)*(n/i)*((n/i)+3);
}
printf("%lld\n",ans);
}
return 0;
}