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<=n∑z<=n[gcd(x,y,z)=1]  -> ∑x<=n∑y<=n∑z<=n∑d|gcd(x,y,z) mu[d]

2. 只存在一个0 ans = 3*(∑x<=n∑y<=n[gcd(x,y)=1]) -> 3*(∑x<=n∑y<=n∑d|gcd(x,y) mu[d])

3.3个数两个为0 ans = 3

那么只要将上面的3个值相加就是我们要求的答案了

 1 #include <cstdio>
 2 #include <cstring>
 3 using namespace std;
 4 const int MAXN = 1000000;
 5 #define ll long long
 6 int mu[MAXN+5] , prime[MAXN/10];
 7 bool check[MAXN+5];
 8
 9 void getMu(int n)
10 {
11     memset(check , 0 ,  sizeof(check));
12     int tot=0;
13     check[1] = true , mu[1]=1 , mu[2]=-1;
14     for(int i=2 ; i<=n ; i++){
15         if(!check[i]) prime[tot++]=i , mu[i]=-1;
16         check[i] = true;
17         for(int j=0 ; j<tot ; j++){
18             if(i*prime[j]>n) break;
19             check[i*prime[j]]=true;
20             if(i%prime[j] == 0){
21                 mu[i*prime[j]] = 0;
22                 break;
23             }else{
24                 mu[i*prime[j]] = -mu[i];
25             }
26         }
27     }
28 }
29
30 int main()
31 {
32    // freopen("a.in" , "r" , stdin);
33     getMu(MAXN);
34     int T , n;
35     scanf("%d" , &T);
36     while(T--)
37     {
38         scanf("%d" , &n);
39         ll ans = 0;
40         for(int i=1 ; i<=n  ; i++){
41             ll tmp1 = (ll)(n/i)*(n/i)*(n/i)*mu[i]; //坐标中一个零都不存在的情况
42             ll tmp2 = (ll)3*(n/i)*(n/i)*mu[i];  //坐标中有一个零的情况
43             ll tmp3 = (ll)3*(n/i)*mu[i]; //坐标中有两个零的情况
44             ans += tmp1+tmp2+tmp3;
45         }
46         printf("%lld\n" , ans);
47     }
48     return 0;
49 }
时间: 2024-10-30 14:16:21

SPOJ - VLATTICE Visible Lattice Points 莫比乌斯反演的相关文章

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