您的位置:首页 > 其它

A - Visible Lattice Points SPOJ - VLATTICE 容斥原理/莫比乌斯反演

2017-05-08 22:09 411 查看
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 :
3
1
2
5

Sample Output :
7
19
175

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


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e6+10;
const int INF=0x3f3f3f3f;
int cas=1,T;
int c
;
void init()
{
memset(c,0,sizeof(c));
c[1]=1;
for(int i=1;i<N;i++)
{
if(c[i]) for(int j=i<<1;j<N;j+=i) c[j]-=c[i];
c[i]+=c[i-1];
}
}
int p
,vis
,pn;
void getMu(int *mu)  //O(n)
{
memset(vis,0,sizeof(vis));
mu[1]=1;
pn=0;
for(int i=2; i<N; i++)
{
if(!vis[i]) { p[pn++]=i;mu[i]=-1; }
for(int j=0; j<pn&&i*p[j]<N; j++)
{
vis[i*p[j]]=1;
if(i%p[j]) mu[i*p[j]]=-mu[i];
else { mu[i*p[j]]=0;break;}
}
mu[i]+=mu[i-1];
}
}
int main()
{
//freopen("1.in","w",stdout);
//freopen("1.in","r",stdin);
//freopen("1.out","w",stdout);
//init();
getMu(c);
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
LL ans=0;
//for(int i=1;i<=n;i++) if(c[i]) ans+=c[i] * ((LL) (n/i+1)*(n/i+1)*(n/i+1)-1);
for(int i=1;i<=n;)
{
int x=n/i;
int y=n/x;
ans+=(c[y]-c[i-1]) * ((LL) (n/i+1)*(n/i+1)*(n/i+1)-1);
i=y+1;
}
printf("%lld\n",ans);
}
//printf("time=%.3lf\n",(double)clock()/CLOCKS_PER_SEC);
return 0;
}


AC 0ms
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: