線篩歐拉函數模板

例題 :jzoj1719 SDOI2008儀仗隊

include <cstdio>
#include <cmath>
#include <iostream>
#include <cmath>
#define fo(i,a,b) for (int i=a;i<=b;i++)
#define N 40005

using namespace std;

typedef long long ll;

int Phi[N],Pre[N],ans = 0;
int Prime[N],tot = 0,n;
bool bz[N];

void PHI()
{
    Phi[1] = 1;
    fo(i,2,n)
    {
        if (!bz[i]) Phi[i] = i - 1,Prime[++ tot] = i;
        fo(j,1,tot)
        {
            int x = Prime[j];
            if (i * x > n) break;
            bz[i * x] = true;
            if (i % x == 0) 
            {
                Phi[i * x] = Phi[i] * x;
                break;
            }
            else Phi[i * x] = Phi[i] * Phi[x];
        } 
    }
}

int main()
{
    scanf("%d",&n);
    PHI();
    fo(i,1,n - 1) ans = ans + Phi[i];
    printf("%d\n",ans * 2 + 1);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章