1. 程式人生 > >洛谷 - P2158 - 儀仗隊 - 歐拉函數

洛谷 - P2158 - 儀仗隊 - 歐拉函數

const eve otp pre 歐拉函數 pac www 對稱 code

https://www.luogu.org/problemnew/show/P2158

好像以前有個妹子收割鏟也是歐拉函數.
因為格點直線上的點,dx與dy的gcd相同,畫個圖就覺得是歐拉函數.但是要註意對稱軸還有左下角那個破點!

#include<bits/stdc++.h>
using namespace std;
#define ll long long

const int MAXN=40000+5;

int phi[MAXN];

int pri[MAXN],pritop;
bool notpri[MAXN];
//pritop從1開始計數

void sieve2(int n) {
    notpri[1]=phi[1]=1;
    for(int i=2; i<=n; i++) {
        if(!notpri[i])
            pri[++pritop]=i,phi[i]=i-1;
        for(int j=1; j<=pritop&&i*pri[j]<=n; j++) {
            notpri[i*pri[j]]=1;
            //略有不同
            if(i%pri[j])
                phi[i*pri[j]]=phi[i]*phi[pri[j]];
            else {
                phi[i*pri[j]]=phi[i]*pri[j];
                break;
            }
        }
    }
}

int main(){
    sieve2(40000);
    int n;
    cin>>n;
    if(n==1)
        cout<<"0"<<endl;
    else{
        ll sumphi=0;
        for(int i=1;i<n;i++){
            sumphi+=phi[i];
        }
        sumphi*=2;
        sumphi+=1;
        cout<<sumphi<<endl;
    }
}

洛谷 - P2158 - 儀仗隊 - 歐拉函數