1 solutions

  • 0
    @ 2024-6-8 15:15:07
    #include<bits/stdc++.h>
    using namespace std;
    const int N=1e6+10;
    bool st[N];
    int p[N],phi[N],cnt;
    long long get(int n)
    {
        phi[1]=1;
        for(int i=2;i<=n;i++)
        {
            if(!st[i]) //质数 
            {   
                p[cnt++]=i; //质数增加 
                phi[i]=i-1; //欧拉函数值 
            }
            for(int j=0;p[j]*i<=n;j++)
            {
                st[i*p[j]]=1; //标记 
                if(i%p[j]==0)
                {
                    //phi[i*p[j]]相对于phi[i]只是n值发生了变化 
                    phi[i*p[j]]=phi[i]*p[j];
                    break;
                }
                else
                {
                    //phi[i*p[j]]相对于phi[i]n值和质因子都发生了变化 
                    phi[i*p[j]]=phi[i]*(p[j]-1);
                }
            }
        }
        long long res=0;
        for(int i=1;i<=n;i++) //累加1到n的欧拉函数值 
        {
            res=res+phi[i];
        }
        return res;
    }
    int main()
    {
        int n;
        cin>>n;
        cout<<get(n);
        return 0;
    }
    
    • 1

    Information

    ID
    186
    Time
    500ms
    Memory
    256MiB
    Difficulty
    5
    Tags
    # Submissions
    12
    Accepted
    5
    Uploaded By