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

参考学长博客 
>>芷水<<

题意:GCD(a,b,c)=1, 0<=a,b,c<=N ;

莫比乌斯反演,十分的巧妙。

GCD(a,b)的题十分经典。这题扩展到GCD(a,b,c)加了一维,但是思想却是相同的。

设f(d) = GCD(a,b,c) = d的种类数 ;

F(n) 为GCD(a,b,c) = d 的倍数的种类数, n%a == 0 n%b==0 n%c==0。

即 :F(d) = (N/d)*(N/d)*(N/d);//N中是d的倍数的个数,然后组合

则f(d) = sigma( mu[n/d]*F(n), d|n )

由于d = 1 所以f(1) = sigma( mu[n]*F(n) ) = sigma( mu[n]*(N/n)*(N/n)*(N/n) );

由于0能够取到,所以对于a,b,c 要讨论一个为0 ,两个为0的情况 (3种).其实又要在开头加两个0的情况;中间算三

个的时候,把其中一个变成0,就是一个0的情况了。

初探莫比乌斯。还有很多不是很懂。跟进中。。。

发现莫比乌斯真的很巧妙呢。。。要多加练习。嗯!

转载请注明出处:寻找&星空の孩子

题目链接:http://www.spoj.com/problems/VLATTICE/   对了:用c++4.3.2交过的

(ps:这里的账号居然我都注册不了,还得借学长的。这算是歧视吗。。。〒_〒)

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = 1e6+10;
typedef long long LL;

LL F[MAXN],f[MAXN];
LL pri[MAXN],pri_num;
LL mu[MAXN];//莫比乌斯函数值
int vis[MAXN];

void mobius(int N)  //筛法求莫比乌斯函数
{
    pri_num = 0;//素数个数
    memset(vis, 0, sizeof(vis));
    vis[1] = mu[1] = 1;
    for(int i = 2; i <=N; i++)
    {
        if(!vis[i])
        {
            pri[pri_num++] = i;
            mu[i] = -1;
        }
        for(int j=0; j<pri_num && i*pri[j]<N ;j++)
        {
            vis[i*pri[j]]=1;//标记非素数
            //eg:i=3,i%2,mu[3*2]=-mu[3]=1;----;i=6,i%5,mu[6*5]=-mu[6]=-1;
            if(i%pri[j])mu[i*pri[j]] = -mu[i];
            else
            {
                mu[i*pri[j]] = 0;
                break;
            }

        }
    }
}

int main()
{
    int T,n;
    scanf("%d",&T);
    mobius(1000005);
    while(T--)
    {
        scanf("%d",&n);
        LL ans = 3;//(0,0,1)(0,1,0)(1,0,0)三个特例
        for(int i=1; i<=n; i++)
            ans+=mu[i]*(n/i)*(n/i)*((n/i)+3);//+3因为有个0的也符合条件
        printf("%lld\n",ans);
    }
    return 0;
}

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

时间: 2024-08-24 10:52:48

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

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

数论 - 欧拉函数的运用 --- 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

POJ 3090 Visible Lattice Points

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 from the origin if the line from (0, 0) to (x, y) does not pass through any other lattice point. For example

POJ 3090 Visible Lattice Points 布雷级数

Visible Lattice Points Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5779   Accepted: 3409 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

poj 3090 Visible Lattice Points 法雷级数||打表

由于图像关于对角线对称,所以我们只看下三角区域.将x轴看做分母,被圈的点看成分子 依次是{1/2},{1/3,1/2},{1/4,3/4},{1/5,2/5,3/5,4/5} 写成前缀和的形式就是 {1/2},{1/2,1/3,2/3},{1/2,1/3,1/3,1/4,3/4},{1/2,1/3,1/3,1/4,3/4,1/5,2/5,3/5,4/5} 发现,这就是一个法雷级数,即第k项增加的数就是phi[k].最后的答案*2+(0,1)+(1,0),(1,1)三个点就好了 #include

POJ 3090 Visible Lattice Points 欧拉函数

链接:http://poj.org/problem?id=3090 题意:在坐标系中,从横纵坐标 0 ≤ x, y ≤ N中的点中选择点,并且这些点与(0,0)的连点不经过其他的点. 思路:显而易见,x与y只有互质的情况下才会发生(0,0)与(x,y)交点不经过其他的点的情况,对于x,y等于N时,可以选择的点均为小于等于N并且与N互质的数,共Euler(N)个,并且不重叠.所以可以得到递推公式aa[i]=aa[i]+2*Euler(N). 代码: #include <iostream> #in

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