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, 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
首先,我们只需要求一个三角形里面的点数,然后*2+1。
然后,碰到这种乍看可能没啥想法的题,果断模拟:
1×1只有一个斜率为0的
2×2斜率有0,1/2(0已经算过了),其实就多了一个斜率为1/2的。
3×3的时候,有1/3,2/3,比以前多了2个
4×4的时候,有1/4,2/4(1/2有过了),3/4,也是2个
5×5的时候,有1/5,2/5,3/5,4/5,之前都没有,多了4个
6×6得到时候,有1/6,2/6(1/3已经有了),3/6(1/2已经有了),4/6(2/3已经有了),5/6,所以只剩2个。
点(x,y)可见当且仅当x,y互质
问题就变成了:求1-N中,所有与N互质的数的个数。
这不就是欧拉函数....
so,ans=(euler(1)+euler(2)+...+euler(n))*2+1。
然后,就木有然后了...
1 #include <stdio.h> 2 #include <string.h> 3 #define nMax 1010 4 5 int C, N; 6 const int M = 1001; 7 int p[M], pNum, phi[M]; 8 bool f[M]; 9 10 //既求N以内的素数以及欧拉数的模版 11 void Prime() 12 { 13 int i, j; 14 for(i = 2; i < M; i++) { 15 if(!f[i]){ 16 p[pNum++] = i; 17 phi[i] = i-1; 18 } 19 for(j = 0; j < pNum && p[j] * i < M; j++ ){ 20 f[p[j]*i] = 1; 21 if(i % p[j] == 0){ 22 phi[i*p[j]] = phi[i] * p[j]; 23 break; 24 } 25 else 26 phi[i*p[j]] = phi[i] *(p[j] - 1); 27 } 28 } 29 } 30 31 int main() 32 { 33 while (scanf("%d", &C) != EOF){ 34 Prime(); 35 phi[1] = 1; 36 for (int i = 0; i < C; ++ i){ 37 scanf("%d", &N); 38 int sum = 0; 39 for (int j = 1; j <= N ; ++ j ) 40 sum += phi[j]; 41 printf("%d %d %d\n", i + 1, N, 2 * sum + 1); 42 } 43 } 44 return 0; 45 }