POJ 3090 欧拉函数

链接:

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

题意:

有一个n*n的二维格点,问在原点(0,0)处能看到多少个格点?

题解:

计算每个数的欧拉函数,然后就是前缀和*2+1就可以了

代码:

31 int getphi(int n) {
32     int ans = n;
33     for(int i = 2; i*i <= n; i++) {
34         if (n%i == 0) {
35             ans -= ans / i;
36             while (n%i == 0) n /= i;
37         }
38     }
39     if (n > 1) ans -= ans / n;
40     return ans;
41 }
42 
43 int a[MAXN];
44 
45 int main() {
46     ios::sync_with_stdio(false), cin.tie(0);
47     rep(i, 1, MAXN) a[i] = getphi(i), a[i] += a[i - 1];
48     int T;
49     cin >> T;
50     rep(cas, 1, T + 1) {
51         int x;
52         cin >> x;
53         cout << cas << ' ' << x << ' ' << a[x] * 2 + 1 << endl;
54     }
55     return 0;
56 }
原文地址:https://www.cnblogs.com/baocong/p/7619978.html