pku3090 Visible Lattice Points

http://poj.org/problem?id=3090

数论,欧拉函数,递推求欧拉函数

 1 #include <stdio.h>
 2 
 3 const int maxn = 1010;
 4 
 5 int minDiv[maxn], phi[maxn], sum[maxn];
 6 
 7 void genPhi()
 8 {
 9     for(int i=1; i<maxn; i++)
10     {
11         minDiv[i] = i;
12     }
13     for(int i=2; i*i<maxn; i++)
14     {
15         if(minDiv[i] == i)
16         {
17             for(int j=i*i; j<maxn; j+=i)
18             {
19                 minDiv[j] = i;
20             }
21         }
22     }
23     phi[1] = 1;
24     for(int i=2; i<maxn; i++)
25     {
26         phi[i] = phi[i / minDiv[i]];
27         if((i/minDiv[i]) % minDiv[i] == 0)
28         {
29             phi[i] *= minDiv[i];
30         }
31         else
32         {
33             phi[i] *= minDiv[i] - 1;
34         }
35     }
36 }
37 
38 int main()
39 {
40     int t, cases, i;
41     genPhi();
42     sum[1] = 3;
43     for(i=2; i<maxn; i++)
44     {
45         sum[i] = sum[i-1] + phi[i]*2;
46     }
47     scanf("%d", &t);
48     for(cases=1; cases<=t; cases++)
49     {
50         scanf("%d", &i);
51         printf("%d %d %d\n", cases, i, sum[i]);
52     }
53     return 0;
54 }

 

posted @ 2013-05-25 21:15  Yuan1991  阅读(165)  评论(0编辑  收藏  举报