Codeforces 906D (Euler's Power Reduction Theorem? + Unique Factorization Theorem)

The inexplicable Euler's power reduction theorem, I can't find proofs and explanations on the Internet, only this blog that I don't understand very well (I feel that the code and text are not related), but the magic can be ac, I feel very strange, is it really me Too much. . .
The code should be correct, although many places cannot be understood, but it can be recorded and used as a template

Code from blog:
#include<bits/stdc++.h>  
#define Mod(a,b) a<b?a:a%b+b //Redefine modulo, according to the conditions of Euler's theorem  
#define LL long long  
#define N 100010  
using namespace std;  
  
LL n,q,mod,a[N];  
map<LL,LL> mp;  
  
LL qpow(LL x,LL n,LL mod)  
{  
    LL res=1;  
    while(n)  
    {  
        if (n&1) res=Mod(res*x,mod),n--;  
        x=Mod(x*x,mod); n>>=1;  
    }  
    return res;  
}  
  
LL phi(LL k)  
{  
    LL i,s=k,x=k;  
    if (mp.count(k)) return mp[x]; // memoized storage  
    for(i = 2;i * i <= k; i++)  
    {  
        if(k % i == 0) s = s / i * (i - 1);  
        while(k % i == 0) k /= i;  
    }  
    if(k > 1) s = s / k * (k - 1);  
    mp[x]=s; return s;  
}  
  
LL solve(LL l,LL r,LL mod)  
{  
    if (l==r||mod==1) return Mod(a[l],mod); //If the right endpoint or the φ value is equal to 1, then return the current number directly  
    return qpow(a[l],solve(l+1,r,phi(mod)),mod); //otherwise the index is the result of the interval [l+1,r]  
}  
  
intmain()  
{  
    scanf("%lld%lld",&n,&mod);  
    for(int i=1;i<=n;i++)  
        scanf("%lld",&a[i]);  
    scanf("%lld",&q);  
    while(q--)  
    {  
        int L, R;  
        scanf("%d%d",&L,&R);  
        printf("%lld\n",solve(L,R,mod)%mod); //Modulo mod, because qpow uses Mod(a,b) to take modulo  
    }  
    return 0;  
}  
There is a piece of code in it:
LL phi(LL k)  
{  
    LL i,s=k,x=k;  
    if (mp.count(k)) return mp[x]; // memoized storage  
    for(i = 2;i * i <= k; i++)  
    {  
        if(k % i == 0) s = s / i * (i - 1);  
        while(k % i == 0) k /= i;  
    }  
    if(k > 1) s = s / k * (k - 1);  
    mp[x]=s; return s;  
}  

The return value is the Euler function value, which is a bit confusing at first, but if you look carefully, it is the only decomposition theorem
Decompose a number, various prime factors, and then use the Euler function to solve it
φ(n)=n*(1-1/p1)(1-1/p2)....(1-1/pk), where p1, p2...pk are all prime factors of n, which can be obtained by Pull the function value, it is worth learning. . The above code should be used as a template first. .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324490388&siteId=291194637