SPOJ VLATTICE Visible Lattice Points 莫比乌斯反演 难度:3

http://www.spoj.com/problems/VLATTICE/

明显,当gcd(x,y,z)=k,k!=1时,(x,y,z)被(x/k,y/k,z/k)遮挡,所以这道题要求的是gcd(x,y,z)==1的个数+{(x,y,0)|gcd(x,y)==1}的个数+3{(0,0,1),(0,1,0),(1,0,0)}

现在不去管最后的三个坐标轴上的点,

设f(i)=|{(x,y,0)|gcd(x,y)==i}|*3+|{(x,y,z)|gcd(x,y,z)==i}|,也就是不在坐标轴上且非0坐标值的最大公约数为n的个数,

设F(i)为由能被i整除的坐标值组成的不在坐标轴上的坐标的个数,则F(i)=n/i*n/i*(n/i+3),同时显然F(i)=sigma(b|n,f[i]),

由莫比乌斯反演,可得f(1)=sigma(mul(i)*F(i))

#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn =1e6+2;
bool ifprime[maxn];
int mul[maxn];
int prime[maxn];
void moblus(){
        ifprime[2]=true;
        for(int i=3;i<maxn;i+=2)ifprime[i]=true;
        int pnum=0;
        mul[1]=1;
        for(int i=2;i<maxn;i++){
                if(ifprime[i]){
                        prime[pnum++]=i;
                        mul[i]=-1;
                }
                for(int j=0;j<pnum&&i*prime[j]<maxn;j++){
                        ifprime[i*prime[j]]=false;
                        if(i%prime[j]==0){
                                mul[i*prime[j]]=0;
                                break;
                        }
                        else {
                                mul[i*prime[j]]=-mul[i];
                        }
                }
        }
}
int main(){
        int T;
        moblus();
        scanf("%d",&T);
        while(T--){
                int n;
                scanf("%d",&n);
                long long ans=3;
                for(int i=1;i<=n;i++){
                                ans+=(long long)mul[i]*(n/i)*(n/i)*(n/i+3);
                }
                printf("%I64d\n",ans);
        }
        return 0;
}

  

时间: 2024-10-20 17:54:07

SPOJ VLATTICE Visible Lattice Points 莫比乌斯反演 难度:3的相关文章

SPOJ - VLATTICE Visible Lattice Points 莫比乌斯反演

题目大意: 从坐标(0,0,0)处观察到所有在(n,n,n)范围内的点的个数,如果一条直线上出现多个点,除了第一个,后面的都视为被遮挡了 这题目稍微推导一下可得知 gcd(x,y,z) = 1的点是可观察到的,若三者的gcd>1,则这个点之前必然出现了一个(x/gcd(x,y,z) , y/gcd(x,y,z) , z/gcd(x,y,z))的点 那么因为 0 是无法计算gcd的 , 所以先不考虑0 1. x>0 , y>0 , z>0  ans = ∑x<=n∑y<=

spoj 7001 Visible Lattice Points莫比乌斯反演

Visible Lattice Points Time Limit:7000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description 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 co

[SPOJ VLATTICE]Visible Lattice Points 数论 莫比乌斯反演

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 lat

SPOJ VLATTICE Visible Lattice Points 初入莫比乌斯

题意:求两个点(x,y,z)的连线不经过其他点有几个 解:即为求GCD(x,y,z)为1的点有几个 解一:因为x,y,z均在1~n内,所以可以用欧拉函数解出 解二:莫比乌斯反演 设f[n]为GCD(x,y,z)=n的个数 设F[b]为b|GCD(x,y,z)的个数,很明显F[b]=(n/i)*(n/i)*(n/i) 所以F[n]=sigema(b|n,f[b]); f[n]=sigema(n|b,mu[n],F[n]) #include <stdio.h> #include <strin

SPOJ VLATTICE - Visible Lattice Points 【“小”大数加减】

题目链接 一道比较简单的莫比乌斯反演,不过ans会爆long long,我是用结构体来存结果的,结构体中两个LL型变量分别存大于1e17和小于1e17的部分 #include<bits/stdc++.h> using namespace std; typedef long long LL; const int maxn=1e6; int prime[maxn+5]; bool check[maxn+5]; int mu[maxn+5]; void init() { mu[1]=1; int t

SPOJ—VLATTICE Visible Lattice Points(莫比乌斯反演)

http://www.spoj.com/problems/VLATTICE/en/ 题意: 给一个长度为N的正方形,从(0,0,0)能看到多少个点. 思路:这道题其实和能量采集是差不多的,只不过从二维上升到了三维. 分三部分计算: ①坐标值上的点,只有3个. ②与原点相邻的三个表面上的点,需满足gcd(x,y)=1. ③其余空间中的点,需满足gcd(x,y,z)=1. 1 #include<iostream> 2 #include<algorithm> 3 #include<

SPOJ1007 VLATTICE - Visible Lattice Points

VLATTICE - Visible Lattice Points no tags 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

Visible Lattice Points(spoj7001+初探莫比乌斯)gcd(a,b,c)=1 经典

VLATTICE - Visible Lattice Points no tags 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

数论 - 欧拉函数的运用 --- poj 3090 : Visible Lattice Points

Visible Lattice Points Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5636   Accepted: 3317 Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), other than the origin, is visible fr