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 <string.h>
const int maxn=1000005;
int prime[maxn];
int num[maxn];
int mu[maxn];
void mobius()
{
    memset(num,0,sizeof(num));
    mu[1]=1;
    int all=0;
    for(int i=2;i<maxn;i++)
    {
        if(!num[i])
        {
            prime[all++]=i;
            mu[i]=-1;
        }
        for(int j=0;j<all&&i*prime[j]<maxn;j++)
        {
            num[i*prime[j]]=1;
            if(i%prime[j])
                mu[i*prime[j]]=-mu[i];
            else
            {
                mu[i*prime[j]]=0;
                break;
            }
        }
    }
    return ;
}
int main()
{
    int t;
    int n;
    long long sum;
    mobius();
    while(scanf("%d",&t)!=-1)
    {
        while(t--)
        {
            sum=3;                       //x,y,z轴
            scanf("%d",&n);
            for(int i=1;i<=n;i++)
            {
                sum+=(long long)mu[i]*(n/i)*(n/i)*((n/i)+3);            //在面上的点,所以+3
            }
            printf("%lld\n",sum);
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-08 23:23:50

SPOJ VLATTICE Visible Lattice Points 初入莫比乌斯的相关文章

[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 莫比乌斯反演 难度: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}|,也就是不在坐标轴上且

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<

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

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