POJ 3090 坐标系上的视线遮蔽问题

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 from the origin if the line from (0, 0) to (x, y) does not pass through any other lattice point. For example, the point (4, 2) is not visible since the line from the origin passes through (2, 1). The figure below shows the points (x, y) with 0 ≤ x, y ≤ 5 with lines from the origin to the visible points.

 

Write a program which, given a value for the size, N, computes the number of visible points (x, y) with 0 ≤ x, yN.

Input

The first line of input contains a single integer C (1 ≤ C ≤ 1000) which is the number of datasets that follow.

Each dataset consists of a single line of input containing a single integer N (1 ≤ N ≤ 1000), which is the size.

Output

For each dataset, there is to be one line of output consisting of: the dataset number starting at 1, a single space, the size, a single space and the number of visible points for that size.

Sample Input

4
2
4
5
231

Sample Output

1 2 5
2 4 13
3 5 21
4 231 32549


这题目求的是视线所及未挡住的点
因为如(4,2)这样的点,gcd(4,2)=2!=1,有(2,1)将其挡住了。所以这题变向再求下x,y互质的点
可以从斜对角线对半分开,算一半最后乘2即可

代码如下:
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <cstring>
 4 using namespace std;
 5 #define LL long long
 6 #define N 1010
 7 int phi[1020];
 8 int F[1020];
 9 
10 void get_phi()
11 {
12     memset(phi,0,sizeof(phi));
13     phi[1]=1;
14     for(int i=2;i<=N;i++)
15     {
16         if(!phi[i])
17             for(int j=i;j<=N;j+=i)
18             {
19                 if(!phi[j]) phi[j]=j;
20                 phi[j]=phi[j]*(i-1)/i;
21             }
22     }
23 }
24 
25 int main()
26 {
27     F[1]=1;
28     get_phi();
29     for(int i=2;i<=N;i++) F[i]=F[i-1]+phi[i];
30     int n,T;
31     cin>>T;
32     for(int i=1;i<=T;i++){
33         cin>>n;
34         cout<<i<<' '<<n<<' '<<F[n]*2+1<<endl;
35     }
36     return 0;
37 }
原文地址:https://www.cnblogs.com/CSU3901130321/p/3863413.html