SPOJ VLATTICE Visible Lattice Points (莫比烏斯反演基礎題)
Visible Lattice Points
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
Added by: | Varun Jalan |
Date: | 2010-07-29 |
Time limit: | 1.368s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel Pentium G860 3GHz) |
Languages: | All except: NODEJS objc PERL 6 VB.net |
Resource: | own problem used for Indian ICPC training camp |
題目連結:http://www.spoj.com/problems/VLATTICE/en/
題目大意:求在(0,0,0)到(n,n,n)這個立方體裡從(0,0,0)能看到多少個點
題目分析:(2,2,2)就看不到,因為被(1,1,1)擋住了,做過能量採集的都知道,就是求gcd(a, b, c) = 1的組數,其中1 <= a, b, c <= n,裸的莫比烏斯反演題,注意兩點,三個數軸上還有三點(0, 0, 1),(0 ,1, 0),(1, 0, 0),另外xoy面,yoz面,xoz面,三個面上還有一些點,這些都要單獨算,然後再加上立方體中不包括軸和麵的點,分塊求和優化10ms解決
#include <cstdio>
#include <algorithm>
#define ll long long
using namespace std;
int const MAX = 1000005;
int mob[MAX], p[MAX], sum[MAX];
bool noprime[MAX];
int Min(int a, int b, int c)
{
return min(a, min(b, c));
}
void Mobius()
{
int pnum = 0;
mob[1] = 1;
sum[1] = 1;
for(int i = 2; i < MAX; i++)
{
if(!noprime[i])
{
p[pnum ++] = i;
mob[i] = -1;
}
for(int j = 0; j < pnum && i * p[j] < MAX; j++)
{
noprime[i * p[j]] = true;
if(i % p[j] == 0)
{
mob[i * p[j]] = 0;
break;
}
mob[i * p[j]] = -mob[i];
}
sum[i] = sum[i - 1] + mob[i];
}
}
ll cal(int l, int r)
{
if(l > r)
swap(l, r);
ll ans = 0;
for(int i = 1, last = 0; i <= l; i = last + 1)
{
last = min(l / (l / i), r / (r / i));
ans += (ll) (l / i) * (r / i) * (sum[last] - sum[i - 1]);
}
return ans;
}
ll cal(int l, int m, int r)
{
if(l > r)
swap(l, r);
if(l > m)
swap(l, m);
ll ans = 0;
for(int i = 1, last = 0; i <= l; i = last + 1)
{
last = Min(l / (l / i), m / (m / i), r / (r / i));
ans += (ll) (l / i) * (m / i) * (r / i) * (sum[last] - sum[i - 1]);
}
return ans;
}
int main()
{
Mobius();
int T;
scanf("%d", &T);
while(T --)
{
int n;
scanf("%d", &n);
ll ans = 3;
ans += (ll) cal(n, n, n);
ans += (ll) cal(n ,n) * 3;
printf("%lld\n", ans);
}
}
相關文章
- SPOJ PGCD - Primes in GCD Table (好題! 莫比烏斯反演+分塊求和優化)GC優化
- HDU 5212 Code (容斥 莫比烏斯反演基礎題)
- ZOJ 3435 Ideal Puzzle Bobble (莫比烏斯反演基礎題)Idea
- 莫比烏斯反演
- Hackerrank GCD Product(莫比烏斯反演)GC
- 莫比烏斯反演學習筆記筆記
- 比較典的莫比烏斯反演
- 狄利克雷卷積 & 莫比烏斯反演卷積
- HDU 4746 Mophues (莫比烏斯反演應用)
- ZOJ 3868 GCD Expectation (容斥+莫比烏斯反演)GC
- HDU 1695 GCD (容斥 + 莫比烏斯反演)GC
- POJ 3904 Sky Code (容斥+莫比烏斯反演)
- 狄利克雷卷積與莫比烏斯反演卷積
- BZOJ 3309 DZY Loves Math (莫比烏斯反演的應用 好題)
- BZOJ 2818 Gcd (莫比烏斯反演 或 尤拉函式)GC函式
- 演算法隨筆——數論之莫比烏斯反演演算法
- 洛谷 P2257 YY的GCD(莫比烏斯反演)GC
- Codeforces 548E Mike and Foam (容斥+莫比烏斯反演)
- CSU 1325 A very hard problem (莫比烏斯反演+分塊求和優化)優化
- Codeforces 235E Number Challenge (神定理+莫比烏斯反演)
- FZU 1969 && UVA 11426 GCD Extreme (尤拉函式 或 莫比烏斯反演)GCREM函式
- 基礎題
- 樹鏈剖分模板+入門題 SPOJ - QTREEQT
- Java基礎題Java
- 基礎題一
- 基礎題二
- Property "visible" must be accessed with "$data.visible"
- Grid Points
- java基礎題(部分)Java
- python基礎題Python
- iOS基礎面試題iOS面試題
- Oracle基礎面試題Oracle面試題
- java基礎面試題Java面試題
- 基於Google earth engine python API做基於影像的定量反演——步驟及問題總結GoPythonAPI
- BZOJ 2301 [HAOI2011]Problem b (容斥+莫比烏斯反演+分塊優化 詳解)優化
- jQuery :visiblejQuery
- Property "visible" must be accessed with "$data.visible" because properties
- JS基礎知識(覆蓋JS基礎面試題)JS面試題