求单个欧拉函数

根据定理:

\[\varphi(n) = n\cdot \prod_{p\mid n}{(1-\frac{1}{p})} \]

直接分解质因数求即可,时间复杂度 \(\mathcal{O}(\sqrt n)\)

代码

auto cal = [&](ll x){
	ll res = x;
	for (ll i=2;i*i<=x;i++){
		if (x%i==0){
			while (x%i==0){
				x/=i;
			}
			res -= res/i;
		}
	}

	if (x>1){
		res -= res/x;
	}
	return res;
};
posted @ 2026-05-21 23:11  kzssCCC  阅读(7)  评论(0)    收藏  举报