From the screen to the Euler linear function, in all likelihood you can understand it!

Motivation for this article: What if there is to grow it? (First wishful thinking about it)
paper Difficulty: universal - → increase +

\ (Part \;. 1 \) . Sieve linear

Not only can screen a prime number, all of the product can function \ (O (n) \) screened out.
\ (PS \) : multiplicative function only for \ (\ FORALL X, Y \ in P \) (where \ (P \) is the set of prime numbers), \ (F (ab &) = F (A) F (B ) \) arithmetical functions.
I do not dismiss your favorite Erichsen screen, but Luo Gu \ (1e8 \) will still be the card for you.
But you can not skip:
Eppendorf screen, full name is what apparently forgot (less than it \ (qwq \) ?), The core idea is to use the existing prime number, multiplied by the current number to obtain a composite number must be .
Below the sieve is a linear prime number:

    judge[1]=true;
    for(int i=2;i<=maxn;i++)
    {
        if(i*i>maxn) continue;
        if(!judge[i])
        {
            for(int j=2*i;j<=maxn;j+=i) judge[j]=true;//是合数
        }
    }

One can understand it.
Complexity Analysis (Does not):
We know the prime number density \ (\ dfrac {n} { \ ln n} \) , then for each number of engagement \ (X \) to screen \ (\ dfrac {maxn} { x} \) times, however he is close to the sum \ (\ log n \) is (difficult died), so it should be \ (O (the n-\ log \ log the n-) \) , similar to the constant (it does not change the fate of his jammed)
Euler found that the great God (which anyone can), some number will be screened several times, that's what he can not \ (O (n) \) causes, such as \ (6 \) , is \ (3 \) screen twice, then we can write:

for(int i=2;i<=maxn;i++)
    {
        if(!judge[i]) prime[++cnt]=i;
        for(int j=1;j<=cnt&&(prime[j]*i)<=maxn;j++)
        {
            judge[i*prime[j]]=true;
            if(i%prime[j]==0) break;
        }
    }

Why only screen again?
Consider if divisible, then there may also be a factor squared off his screen, throw back to the good.
So complexity is \ (O (n) \) , and then the card would not work.

\ (Part \; 2 \) . Euler function

Euler defined function \ (\ psi (n) \ ) with the \ (n \) are relatively prime number, a thief put a good big brother:
\ (\ PSI (6) \) ye count? Here can be enumerated, is \ (O (the n-\ the n-log) \) , he spoke good way:
write all the denominator is \ (6 \) is not greater than \ (1 \) scores.
\ [\ dfrac {1} { 6}, \ dfrac {2} {6}, \ dfrac {3} {6}, \ dfrac {4} {6}, \ dfrac {5} {6}, \ dfrac { 6} {6} \]
after simplifying packet:
\ [\ dfrac {. 1} {6}, \ dfrac {. 1} {. 3}, \ dfrac {. 1} {2}, \ dfrac {2} {. 3}, \ dfrac {5} {6}
, \ dfrac {1} {1} \] then:
\ [\ PSI (. 6) = 2, \ PSI (. 3) = 2, \ PSI (2) =. 1, \ PSI ( 1) = 1 \]
then it means:
\ [\ sum_ {d | the n-} \ PSI (d) = the n-\]
is clearly observed induction is not tight, if I had the opportunity to learn and be a counter-Mo certificate it again.

\ (Part \;. 3 \) . Seeking linear

Euler function is a multiplicative function, to ensure that it can be \ (O (n) \) screened out, but the principle is more complex, it is recommended to not learn \ (qwq \) , read the following much better,Anyway, I

\ (Part \;. 4 \) . Euler number of a single function

Consider an event:
\ [\ text {if} x \ in P, \ psi
(x) = x-1 \] Only not prime itself, the nature of what we call a.

Nature II: Consider a composite number \ (A \) satisfies \ (A = P ^ K (K>. 1, K \ in the Z) \) , obviously with his prime number \ (b \ in [p, 2p, ^ {k ...... the p-3P-the p-1}] \) (Give me a break, I do not play braces).
Then \ (\ PSI (A) = ^ P ^ {K-KP. 1} = K ^ P (l- \ dfrac {} {P}. 1) \) .

Three properties: the most general case, the number of each co \ (A \) can be written unique factorization formula:
\ [P_1 = A ^ {^ {P_2 K_2 k_1}} ^ {...... P_n K_n} \ ]
That is:
\ [a = \ prod_. 1 = {I} ^ {^ n-K_i P_i} \]
Fortunately Euler function is a special function of the product, to meet the \ (\ psi (nm) = \ psi (n) \ PSI (m) \) , set up (of course domain).
Then
\ [\ psi (a) = \ prod_ {i = 1} ^ n \ psi (p_i ^ {k_i}) = \ prod_ {i = 1} ^ n p_i ^ {k_i} \ prod_ {i = 1} ^ n (1- \ dfrac {1}
{p_i}) = a \ prod_ {i = 1} ^ n (1- \ dfrac {1} {p_i}) \] here, we can \ (O (\ sqrt {n}) \) derived function of a number of Euler. In fact, we had previously thought and learning prime factor is similar to the enumeration verification:
Code:

int phi(int n)
{
    int ans=n;
    for(int i=2;i<=sqrt(n);i++)
    {
        if(n%i==0)
        {
            ans=ans/i*(i-1);//这是一个p,注意先除再乘,防止炸int
            while(n%i==0) n/=i; //把质数次方因子筛没了,就不会错了
        }
    }
    if(n>=2) ans=ans/n*(n-1);//最后有可能剩下
    return ans;
}

But some people do not write:

int phi(int n)
{
    int ans=1,now;
    for(int i=1;i<=sqrt(n);i++) 
    {
        now=1;
        if(n%i==0)
        {
            now=i-1,n/=i;
            while(n%i==0) now*=i,n/=i;
        }
        ans*=now;
    }
    if(n!=1) ans*=n-1;
    return ans;

Under Briefly, for a fraction ( \ (P ^ K \) ) consider modification:
\ [\ PSI (K ^ P) = KP ^ P ^ {} =. 1 K-K-P. 1} ^ {(p- 1) \]
then find prime numbers appear \ (p \) first by a \ (the p--1 \) , again \ (k-1 \) times \ (p \) , the other are the same, but here:

now=i-1,n/=i;

Note that in addition to it, then \ (now, ans \) were recorded, can not be confused.

\ (Part \; 5 \) return. \ (Part \; 3 \)

The remaining like to say.

phi[1]=1;
    for(int i=2;i<=n;i++)
    {
        if(!vis[i]) p[++cnt]=i,phi[i]=i-1;//质数只有自身与自己不互质 
        for(int j=1;p[j]&&i*p[j]<=n;j++)
        {
            vis[i*p[j]]=1;
            if(!(i%p[j]))
            {
                phi[i*p[j]]=phi[i]*p[j];
                //这里是平方因子了,不要减1 
                break;
            }
            else phi[i*p[j]]=phi[i]*(p[j]-1);//第一次出现因子,乘p-1 
        }
    }

In essence the code.
In fact, the screen and the prime number is the same.
Linear sieve but very \ (NB \) , so that all multiplicative function are afraid of him (to cover up the seemingly large staggering complexity of the not).

\ (Part \; 6 \) Euler's theorem

Pre-knowledge: Fast power.
Probably written, complexity is \ (O (\ log n) \) is.

ll quickpow(ll a,ll b)
{
    ll ans=1,base=a;
    while(b!=0)
    {
        if(b&1!=0)//b%2==1;
        {
            ans*=base;
        }
        base*=base;
        b>>=1;//b/=2;
    }
    return ans;
}

You can even define your own powers of multiplication to run faster.
But how to do it big index?
Even \ (O (\ log n) \) would go ah!
Euler made great theorem, we have to remember him, known as Euler's theorem :

If \ (\ GCD (A, m) =. 1 \) , then:
\ [^ {A \ PSI (m)} \ equiv1 \ PMOD {m} \]
he believes not, proposed the expansion of Euler's theorem :
\ [a ^ b \ equiv \ begin {cases} a ^ b & b <\ psi (m) \\ a ^ {b \; mod \; \ psi (m) + \ psi (m)} & b \ geqslant \ psi ( m) \ end {cases} \
] Thus, the first pre-treatment \ (\ PSI (m) \) , then die in the string being read.
the complexityprobablyIt is \ (O (Len_B + \ sqrt {m} + \ log m) \) (after all, \ (\ psi (m) \ ) upper bound is \ (m \) ), by this question.
Code:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int phi(int x)//欧拉函数
{
    int ans=1,num=1;
    for(int i=2;i*i<=x;i++)
    {
        if(!(x%i))
        {
            num=i-1,x/=i;
            while(!(x%i)) num=num*i,x/=i;
            ans=num*ans;
        }
    }
    if(x!=1) ans=ans*(x-1);
    return ans;
}
inline int read(int mod)//改进快读,让他边读边输入
{
    //g用来判断b与phi(m)的大小,如果小于,就不能加了,这是坑点!
    int x=0;
    bool g=false;
    char c=getchar();
    while(c<'0'||c>'9') c=getchar();
    while(c>='0'&&c<='9')
    {
        x=(x<<3)+(x<<1)+(c^'0');
        if(x>=mod) x%=mod,g=true;
        c=getchar();
    }
    if(g) return (x+mod);
    else return x;
}
int a,mod;
char b[20000005];
inline int quickpow(int a,int b)//快速幂
{
    long long ans=1,base=(long long)a;
    while(b)
    {
        if(b&1) ans=ans*base%mod;
        b>>=1;
        base=base*base%mod;
    }
    return (int)(ans%mod);
}
int p;
int main()
{
    scanf("%d%d",&a,&mod);
    int p=phi(mod);
    int cishu=read(p);//得出的化简次数
    int s=quickpow(a,cishu);
    printf("%d\n",s);
    return 0;
}

Probably after these friends, but also some of the things even more profound point.

Guess you like

Origin www.cnblogs.com/tlx-blog/p/12369301.html