spoj 7001. Visible Lattice Points(莫比乌斯反演)

SPOJ Problem Set (classical)

7001. Visible Lattice Points

Problem code: VLATTICE

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

题目链接

/*
还以为上一道题是最后一题呢,趁着还没过年,再来一题。。。,这个肯定是最后一题了,不说谎。。。嘿嘿,,,加油!!!
Time:2014-12-31 23:17
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int MAX=1000000+10;
int prime[MAX+10],mu[MAX+10];
bool vis[MAX+10];
void Mobius(){
    vis[1]=true;int cnt=0;mu[1]=1;
    for(int i=2;i<=MAX;i++){
        if(!vis[i]){
            mu[i]=-1;
            prime[cnt++]=i;
        }
        for(int j=0;j<cnt;j++){
            if(i*prime[j]>MAX)break;
            vis[i*prime[j]]=true;
            if(i%prime[j]==0){
                mu[i*prime[j]]=0;
                break;
            }
            mu[i*prime[j]]=-mu[i];
        }
    }
}
int main(){
    int T,n;
    Mobius();
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        LL ans=3;//3个(1,0,0)(0,1,0)(0,0,1)
        for(int i=1;i<=n;i++){
           ans+=(LL)(n/i)*(n/i)*((n/i)+3)*mu[i];//z方向上每个与x,y方向少计算3个(0,1)(1,0)(0,0)
        }
        printf("%lld\n",ans);
    }
return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值