[bzoj2190][SDOI2008]仪仗队 ——欧拉函数

题解

以c点为(0, 0)建立坐标系,可以发现,
当(x,y)!=1,即x,y不互素时,(x,y)点一定会被点(x/n, y/n)遮挡。
所以点(x, y)被看到的充分必要条件是Gcd(x, y) == 1;
我们考察矩阵的下三角形,考察他的每一行,可以发现,这一行能够被看到的点的数目就是(phi(x))
答案不难发现是(sum(phi[x]) * 2 + 1)(容斥原理)

代码

#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e4+10;
int n, phi[maxn];
void get_phi(int n) {
    phi[1] = 1;
    for(int i = 2; i <= n; i++) if(!phi[i]) {
        for(int j = i; j <= n; j += i) {
            if(!phi[j]) phi[j] = j;
            phi[j] = phi[j] / i * (i-1);
        }
    }
}
int main() {
    scanf("%d", &n);
    get_phi(n);
    int ans = 0;
    for(int i = 1; i < n; i++) ans += phi[i];
    printf("%d", ans * 2 + 1);
    return 0;
}
原文地址:https://www.cnblogs.com/gengchen/p/6349927.html