Description
A lattice point (x, y) in the first quadrant (x andy 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, y ≤N.
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
题意:一个(n+1)*(n+1)的点阵,问多少点能被点(0,0)看到。如果(0,0)到(i,j)的连线被点挡住就算看不到。
用数组ans[i]记录(i+1)*(i+1)的点阵可以看到的点数,则x=y=i的点肯定不能看到,考虑x>y的情况,很容易发现规律:点(x,y)能被发现只有当gcd(x,y)=1时,否则会被点
(x/gcd(x,y),y/gcd(x,y))挡住,然后只需要计算i的欧拉函数值,即点(i,i)的下方有多少点满足条件,再加上x<y的情况(对称的),所以ans[i]=ans[i-1]+2*phi[i]。
代码:
#include<stdio.h> #include<string.h> #include<math.h> int ans[1005]; int eular(int n) { int s,i,m; m=(int)sqrt(n+0.5); s=n; for(i=2;i<=m;i++) if(n%i==0) { s=s/i*(i-1); while(n%i==0) n/=i; } if(n>1) s=s/n*(n-1); return s; } int main() { int n,i,t,cas=1; scanf("%d",&t); ans[1]=3; for(i=2;i<=1000;i++) ans[i]=ans[i-1]+eular(i)*2; while(t--) { scanf("%d",&n); printf("%d %d %d\n",cas++,n,ans[n]); } return 0; }