NC20313 [SDOI2008]仪仗队

https://ac.nowcoder.com/discuss/926597
欧拉筛求欧拉函数
x和y互质的坐标会被看到,可以转化成欧拉函数来做

点击查看代码
#include <bits/stdc++.h>
using namespace std;

#define int long long

int cnt = 0;
bool vis[50004];
int prim[50004];
int phi[50004];
void sieve(int n)
{
    vis[1] = 1;phi[1] = 1;
    for(int i=2;i<=n;++i)
    {
        if(vis[i]==0){
            prim[++cnt] = i;
            phi[i] = i - 1;
        }
        for(int j=1;j<=cnt&&i*prim[j]<=n;++j)
        {
            vis[i*prim[j]] = 1;
            if(i%prim[j]==0){
                phi[i*prim[j]] = phi[i] * prim[j];
                break;
            }
            else phi[i*prim[j]]=phi[i]*phi[prim[j]];
        }
    }
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);

    int n;cin >> n;
    if(n==1){cout << 0 << endl;return 0;}
    sieve(n-1);
    int ans = 0;
    for(int i=1;i<=n-1;++i)
    {
        ans += phi[i];
    }
    cout << 2*ans+1 << endl;

    return 0;
}


posted @ 2022-07-09 17:37  HIVM  阅读(12)  评论(0编辑  收藏  举报