Visible lattice points
Time limit:7000 Ms
Memory limit:0 KB
64bit Io format:% LLD & % llusubmit status
Description
Consider a n * n lattice. one corner is at (0, 0) and the opposite one is at (N, N, N ). how many lattice points are visible from corner at (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
How many points can be seen from the straight line starting from (0, 0, 0) (only the first point can be seen, and the following points are regarded as blocked and invisible ).
Solution: Find the number of gcd (x, y, z) = 1 points, F (n) indicates the gcd (x, y, z) that meets the conditions) = (x, y, z) Logarithm of N; G (n) represents (x, y, z) Logarithm of N | gcd (x, y, z, that is, the logarithm of gcd (x, y, z) % N = 0 (x, y, z;
Defined by: G (n) = sigma (f (d), F (n) = sigma (U (D/n) * G (d ))
This question is F (1 ). G (d) = (N/d) * (N/d ).
When the three coordinates are 0, there are 0 points;
2. When the coordinates are 0, there are three visible points on the three axes;
When the 1 coordinate is 0, 3 * ans (ANS = sigma (U (d) * (N/I )));
When the coordinates are not 0, ANS = ans = sigma (U (d) * (N/I ))
Tip: When submitting code, you cannot use _ int64 or long.
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 typedef __int64 LL; 7 const int maxn=1000005; 8 int prime[maxn],mu[maxn],num; 9 bool flag[maxn];10 11 void init()12 {13 int i,j;num=0;mu[1]=1;14 memset(flag,true,sizeof(flag));15 for(i=2;i<maxn;i++)16 {17 if(flag[i])18 {19 prime[num++]=i;mu[i]=-1;20 }21 for(j=0;j<num&&prime[j]*i<maxn;j++)22 {23 flag[i*prime[j]]=false;24 if(i%prime[j]==0)25 {26 mu[i*prime[j]]=0;break;27 }28 else mu[i*prime[j]]=-mu[i];29 }30 }31 }32 33 int main()34 {35 init();36 int t,i,n;37 scanf("%d",&t);38 while(t--)39 {40 scanf("%d",&n);41 LL ans=3;42 for(i=1;i<=n;i++)43 ans+=(LL)mu[i]*(n/i)*(n/i)*(n/i+3);44 printf("%I64d\n",ans);45 }46 return 0;47 }