VLATTICE - Visible Lattice Points [Spoj 7001]

45 篇文章 0 订阅
2 篇文章 0 订阅

题目地址——


VLATTICE - Visible Lattice Points


如果不想看英文,请点击——


【题目描述】

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 if no other lattice point lies on the segment joining X and Y.


【输入描述】

The first line contains the number of test cases T .
The next T lines contain an interger N .


【输出描述】

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 .


【Solution】

本题题意(让我来当一回翻译):
就是一个 nnn 的空间,一个人在 (0,0,0) ,问他能够看到几个点。

用莫比乌斯反演就很easy了。

f(d)=d|iμ(id)ni2(ni+3)
ans=3+f(1)


【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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值