跳到正文

个人记录

Maths about Phi​ and Mod

Garry1204约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 3 分钟

版本

  1. v1当前

Maths about Phi​ and Mod

About Phi :

ϕ\phi (n) = number of all the integer x that 0 < x < n and gcd(x,n)=1

Change:

From n = p1^c1 p2^c2 ... pm^cm

So ϕ\phi (n) = n(1-1/p1)(1-1/p2) ... (1-1/pm)

How to work out all the ϕ\phi from 0 to n in O(n) ?

C++
int cnt=1,tim,not_prime[1010],sum[1010];
void Euler_Sieve(int range){
    not_prime[1]=1;
	for(int pointer=2;pointer<=range;pointer+=1){
		if(not_prime[pointer]==0){
            sum[pointer]=sum[pointer-1]+pointer-1;
        }
        else {
            int i=pointer/not_prime[pointer],j=not_prime[pointer];
            if(i%j==0)sum[pointer]=sum[pointer-1]+j*(sum[i]-sum[i-1]);
            else sum[pointer]=sum[pointer-1]+(j-1)*(sum[i]-sum[i-1]);
        }
        for(long long i=1;;i+=1){
			if(1LL*i*pointer>1LL*range)break;
			if(not_prime[1LL*i*pointer]==0)not_prime[1LL*i*pointer]=pointer;
		}
	}
}

ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi ϕ\phi

Man!

About Mod :

Man!

原文