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<cstring>
 4 #include<cstdio>
 5 #include<sstream>
 6 #include<vector>
 7 #include<stack>
 8 #include<queue>
 9 #include<cmath>
10 #include<map>
11 #include<set>
12 using namespace std;
13 typedef long long ll;
14 typedef pair<int,int> pll;
15 const int INF = 0x3f3f3f3f;
16 const int maxn = 1000000 + 5;
17
18 bool check[maxn];
19 int prime[maxn];
20 int mu[maxn];
21 ll sum[maxn];
22
23 void Mobius()
24 {
25     memset(check, false, sizeof(check));
26     mu[1] = 1;
27     int tot = 0;
28     for (int i = 2; i <= maxn; i++)
29     {
30         if (!check[i])
31         {
32             prime[tot++] = i;
33             mu[i] = -1;
34         }
35         for (int j = 0; j < tot; j++)
36         {
37             if (i * prime[j] > maxn)
38             {
39                 break;
40             }
41             check[i * prime[j]] = true;
42             if (i % prime[j] == 0)
43             {
44                 mu[i * prime[j]] = 0;
45                 break;
46             }
47             else
48             {
49                 mu[i * prime[j]] = -mu[i];
50             }
51         }
52     }
53     sum[0]=0;
54     for(int i=1;i<maxn;i++)
55         sum[i]=sum[i-1]+mu[i];
56     return ;
57 }
58
59 ll compute1(int n)
60 {
61     ll tmp=0;
62     for(int i=1,last=0;i<=n;i=last+1)
63     {
64         last=n/(n/i);
65         tmp+=(sum[last]-sum[i-1])*(n/i)*(n/i)*(n/i);
66     }
67     return tmp;
68 }
69
70 ll compute2(int n)
71 {
72     ll tmp=0;
73     for(int i=1,last=0;i<=n;i=last+1)
74     {
75         last=n/(n/i);
76         tmp+=(sum[last]-sum[i-1])*(n/i)*(n/i);
77     }
78     return tmp;
79 }
80
81 int n, m;
82
83 int main()
84 {
85     //freopen("in.txt","r",stdin);
86     Mobius();
87     int T;
88     scanf("%d",&T);
89     while(T--)
90     {
91         scanf("%d",&n);
92         printf("%lld\n",compute1(n)+compute2(n)*3+3);
93     }
94     return 0;
95 }
时间: 2024-09-30 10:50:05

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

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