BZOJ 3884-- Euler descending and descending generalized Euler

Theoretical part

Euler's theorem: If $ a, n $ is a positive integer, and $ a, n $ prime, then $ a ^ {\ varphi (n)} \ equiv 1 (mod \ n) $.

Descending 幂公 formula:

$$a^b=
\begin{cases}
a^{b \% \varphi(p)} &  gcd(x,p)=1 \\
a^b &  gcd(a,p)\neq 1,b < \varphi (p) \\
a^{b\% \varphi (p) + \varphi (p)} & gcd(a,p)\neq 1,b \geq \varphi (p)
\end{cases}$$

topic

Request $ ^ 2 ^ {2} {2} ... \ value mod \ p $ a, $ T $ query group. $ T \ leq 1000, p \ leq {10} ^ 7 $

 analysis:

First, it must truly endless and infinite mode under clear sense there is a difference, (or how to find infinite value

Descending by the formula, when the time $ $ x \ geq \ varphi (p),

$$a^x \equiv a^{x \% \phi (p) + \varphi (p)}(mod \ p)$$

Therefore, so $ f (p) = 2 ^ {2 ^ {2 ...}} (mod \ p) $, $ f (1) = 0 $, 

\\ $$ f (p) = 2 ^ {(2 ^ {2 ^ {...}} mod \; \ phi (p)) + \ phi (p)} mod \; p \\ = 2 ^ {f (\ phi (p)) + \ phi (p)} mod \ p $$

So it can be solved recursively.

The time complexity is how much?

Seeking $ \ phi (p) $ is $ \ sqrt p $, were $ \ varphi (\ varphi (... \ varphi (p))) = O (logp) $ times until 1,

So the overall complexity is $ O (\ sqrt p log p) $

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
int p;

ll qpow(ll a, ll b, ll p)
{
    ll ret = 1;
    while(b)
    {
        if(b&1) ret = ret*a%p;
        a = a*a%p;
        b >>= 1;
    }
    return ret;
}

int euler_phi(int n)
{
    int m = (int)sqrt(n + 0.5);
     Int ANS = n-;
     for ( int I = 2 ; I <= m; I ++ ) 
    { 
        IF (n-% I == 0 ) 
        { 
            ANS = ANS / I * (I - . 1 );
             the while (n-% I = = 0 ) n-/ = I;         // divisible 
        } 
    } 
    IF (n-> 1 ) = ANS ANS / n-* (n-- 1 );     // the rest is not 1, the number is prime 
    return ANS; 
} 

int F ( int P) 
{
    if(p == 1)  return 0;
    int phip = euler_phi(p);
    return qpow(2, f(phip)+phip, p);
}

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d", &p);
        printf("%d\n", f(p));
    }
    return 0;
}

 

 

Reference links:

1. https://blog.csdn.net/skywalkert/article/details/43955611

2. https://blog.csdn.net/qq_37632935/article/details/81264965

Guess you like

Origin www.cnblogs.com/lfri/p/11297656.html