跳到正文

个人记录

欧拉函数

yqzx0307约 1 分钟
原文

这是 2026年7月1日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年2月5日
收录
2026年7月1日
更新
2026年11月14日
篇幅
约 1 分钟

版本

  1. v2当前
  2. v1
C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
constexpr int maxn=1e8+10;
constexpr int maxm=3e5+10;
constexpr int INF=1e18;
constexpr double pi=acos(-1.0);
constexpr double eps=1e-10;
int n;
int phi()
{
    int res=n;
    if(!(n&1))
    {
        res>>=1;
        while(!(n&1))
        {
            n>>=1;
        }
    }
    for(int i=3;i*i<=n;i+=2)
    {
        if(n%i==0)
        {
            res-=res/i;
            while(n%i==0)
            {
                n/=i;
            }
        }
    }
    if(n>1)
    {
        res-=res/n;
    }
    return res;
}
signed main()
{
    scanf("%lld",&n);
    printf("%lld",phi());
    return 0;
}