您的位置:首页 > 其它

VLATTICE - Visible Lattice Points [Spoj 7001]

2016-06-05 11:19 344 查看
题目地址——

VLATTICE - Visible Lattice Points

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

【题目描述】

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 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】

本题题意(让我来当一回翻译):

就是一个 n∗n∗n 的空间,一个人在 (0,0,0),问他能够看到几个点。

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

f(d)=∑d|iμ(id)⋅⌊ni⌋2⋅(⌊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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: