Vlattice-visible Lattice Points
No Tags
Consider a n*n*n lattice. One corner is at (0,0,0) and the opposite one are at (n,n,n). How many lattice points is visible from corner at (0,0,0)? A Point X was 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
+
175
Constraints:
T <=
1 <= N <= 1000000
refer to Seniors blog >> Zhi shui <<
Test instructions: GCD (a,b,c) =1, 0<=a,b,c<=n;
Möbius inversion, very ingenious.
GCD (A, b) is a classic question. The problem extends to GCD (A,B,C) with one dimension, but the idea is the same.
Set F (d) = GCD (a,b,c) = number of species of D;
F (n) is the number of multiples of gcd (a,b,c) = d, n%a = = 0 n%b==0 n%c==0.
That is: F (d) = (n/d) * (n/d) * (N/D),//n is the number of multiples of D, and then combined
Then F (d) = Sigma (Mu[n/d]*f (n), d|n)
Because d = 1, F (1) = Sigma (Mu[n]*f (n)) = Sigma (mu[n]* (n/n) * (n/n) * (n/n));
Since 0 can be taken, so for A,b,c to discuss a case of 0, two for 0 (3 kinds). In fact, in the beginning plus two 0 of the case; the middle Count three
, turning one of them into 0 is a 0 situation.
Preliminary study on Möbius. There are many not very understand. Follow-up ...
Find Möbius really ingenious ... Practice more. Well!
Reprint Please specify the Source: Looking for Children & stars
Topic Link: http://www.spoj.com/problems/VLATTICE/: With c++4.3.2 handed over the
(PS: The account here is incredibly I can not register, but also to borrow seniors.) Is this discrimination? 〒_〒)
#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];//function Value int vis[maxn];void mobius (int N)//sieve method for Möbius function {pri_num = 0;//number of 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;//Mark non-prime number/ /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) three exceptions for (int i=1; i<=n; i++) ans+=mu[i]* (n/i) * (n/i) * ((n/i) +3);//+3 because there is also a 0 eligible printf ("%lld\n", ans); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Visible Lattice Points (spoj7001+ preliminary Möbius) gcd (a,b,c) =1 Classic