Euler function entry understanding

meaning

Euler function: phi (n) 1-n n and the number of sections which are relatively prime number

official

First to resolve the most simple qualitative factors Riding

n=p1^a1*p2*a2.....*px^ax

Then that formula is

phi(n)=n*((p1-1)/p1)*((p2-1)/p2)....((px-1)/px)=n*(1-1/p1)*(1-1/p2).....(1-1/px)

Resolve

First, why is this formula it first requires relatively prime, then that after formation of the most simple formula, not have the same quality factor between two numbers

Then we count the next number and 1-n p1 this factor there, so the number is n / p1, p2 count how many factors of the number is the same reason

But we must also add our number deleted twice, that is, contain a number of factors p2 p1 *, so our formula is

Assuming that only two prime factors, then

phi(n)=n-n/p1-n/p2+n/(p1*p2) = n*(1-1/p1-1/p2+1/(p1*p2)) = n*(1-1/p1)*(1-1/p2)

This turned it into a formula, this is only the case of two numbers, of course, be extended to multiple numbers are the same, the middle is to use the theorem of inclusion-exclusion

 

So we write the code as long as all of the prime factors of n is determined to

 

#include<cstdio>
#include<iostream>
#include<cmath>
#define maxn 200005
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ll n;
ll phi(ll x){
    ll sum=x;
    for(int i=2;i<=sqrt(x);i++){
        if(x%i==0){
            SUM = SUM / i * (I- . 1 );
             the while (X% i == 0 ) X / = i; i // the factors are divisible, as well as each quality factor power, we have to get rid of all
        }
    }
    IF (X> . 1 ) SUM = SUM / X * (X- . 1 ); // this last is likely to exceed the quality factor sqrt, except it is not to
     return SUM;
}
int main(){
    while(cin>>n){
        cout<<phi(n)<<"\n";
    }
} 

 

Guess you like

Origin www.cnblogs.com/Lis-/p/11004617.html