spoj 7001 Visible Lattice Points莫比乌斯反演

时间:2022-02-17 23:36:38
Visible Lattice Points

Time Limit:7000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Submit Status

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 : 




 
Sample Output : 

19 
175 
 
Constraints : 
T <= 50 
1 <= N <= 1000000

题目大意:从点(0,0,0)出发的直线可看到多少个点(只能看到第一个,后面的视为挡住了看不见)。

解题思路:求gcd(x,y,z)=1的点有多少个,F(n) 表示满足条件的 gcd(x,y,z)==n的 (x,y,z) 对数;G(n) 表示满足 n | gcd(x,y,z) 的(x,y,z)对数,即 gcd(x,y,z)%n==0 的(x,y,z) 对数;

由定义:G(n)=sigma(F(d)),F(n)=sigma(U(d/n)*G(d))

这题就是求F(1)。G(d)=(n/d)*(n/d)(n/d)。

当3个坐标为0时有0个点;

2坐标为0的时候可见点在三条坐标轴上一共3个;

1坐标为0的时候3*ans(ans=sigma(u(d)*(n/i)*(n/i)));

坐标都不为0的时候ans=ans=sigma(u(d)*(n/i)*(n/i)*(n/i))

提示:提交代码时不能用__int64,只能用long long 

 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; typedef __int64 LL;
const int maxn=;
int prime[maxn],mu[maxn],num;
bool flag[maxn]; void init()
{
int i,j;num=;mu[]=;
memset(flag,true,sizeof(flag));
for(i=;i<maxn;i++)
{
if(flag[i])
{
prime[num++]=i;mu[i]=-;
}
for(j=;j<num&&prime[j]*i<maxn;j++)
{
flag[i*prime[j]]=false;
if(i%prime[j]==)
{
mu[i*prime[j]]=;break;
}
else mu[i*prime[j]]=-mu[i];
}
}
} int main()
{
init();
int t,i,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
LL ans=;
for(i=;i<=n;i++)
ans+=(LL)mu[i]*(n/i)*(n/i)*(n/i+);
printf("%I64d\n",ans);
}
return ;
}