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


一道裸的莫比乌斯反演,一个点 (x,y,z) 与原点连接的线段上没有其他的点的条件是 gcd(x,y,z)==1,然后对于x y z都不为0 ,其中有一个是0,有两个是0用莫比乌斯反演即可


注:f(d)表示gcd(x,y,z)=d的有多少

F(d)表示gcd(x,y,z)是d的倍数的有多少,显然 F(d)=(n/d)^3+3*(n/d)^2+3*(n/d); (分别表示x y z都不为0 ,其中有一个是0,有两个是0)


#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
const LL MOD=1e9+7;
#define fi first
#define se second
const int MAXN=1000000+23;
LL prime[MAXN],total,miu[MAXN];
bool isprime[MAXN];
void make()
{
    int m=MAXN-3;
    memset(isprime,true,sizeof(isprime));
    total=0;
    isprime[0]=isprime[1]=false;
    miu[1]=1;
    for(int i=2;i<=m;i++)
    {
        if(isprime[i]){prime[++total]=i;miu[i]=-1;}
        for(int j=1;j<=total&&prime[j]*i<=m;j++)
        {
            isprime[i*prime[j]]=false;
            if(i%prime[j])
            {
                miu[i*prime[j]]=-miu[i];
            }
            else
            {
                miu[i*prime[j]]=0;
                break;
            }
        }
    }
    miu[0]=0;
    for(int i=1;i<=m;i++)miu[i]+=miu[i-1];
}
LL n;
int main()
{
    make();
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lld",&n);
        LL ans=0;
        for(LL i=1,last=0;i<=n;i=last+1)
        {
            last=n/(n/i);//块端点
            LL z=n/i;
            ans+=(miu[last]-miu[i-1])*(z*z*z+3*z*z+3*z);
        }
        //for(int i=1;i<=n;i++){int z=n/i;ans+=(miu[i])*(z*z*z+3*z*z+3*z);}
        printf("%lld\n",ans);
    }
    return 0;
}

本人蒟蒻,如有错误,还望指正


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值