传送门1
传送门2
description
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
这道题算POJ3090 Visible Lattice Points的升级版。因为这道题是三维的,而POJ的那个是二维的。
而对于这个题我们需要计算gcd(i,j,k)==1就行了,原理和上面那道题是一样的,具体推公式就不去推了,可以点击上方的那个链接看看
而这个题我们首先要考虑的是三个坐标轴上的点,所以ans初始化为3,而我们计算的gcd(i,j,k)==1是对于空间中的,而不包含三个坐标轴平面
所以我们还需要加三个坐标轴平面的点的个数
AC代码如下:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
int miu[maxn];
int vis[maxn];
void mobius(){
for(int i=1;i<=maxn;i++)miu[i]=1;
for(int i=2;i<maxn;i++){
if(!vis[i]){
miu[i]=-1;
for(int j=i+i;j<maxn;j+=i){
vis[j]=1;
if((j/i)%i==0)miu[j]=0;
else miu[j]=-miu[j];
}
}
}
for(int i=1;i<=maxn;i++){
miu[i]+=miu[i-1];
}
}
int main()
{
mobius();
int t;
cin>>t;
for(int i=1;i<=t;i++)
{
long long n,r=0,ans=3;
cin>>n;
for(int j=1;j<=n;j=r+1){
r=n/(n/j);
ans+=(miu[r]-miu[j-1])*((n/j)*(n/j)*(n/j)+3*(n/j)*(n/j));
}
cout<<ans<<endl;
}
return 0;
}
本文探讨了一道升级版的三维可见格点问题,详细解释了如何通过计算gcd(i,j,k)==1来找出从(0,0,0)角可见的所有格点。介绍了使用Mobius函数优化计算过程的方法,并提供了完整的AC代码实现。
241

被折叠的 条评论
为什么被折叠?



