扩展欧拉定理的应用

题意:https://www.lydsy.com/JudgeOnline/problem.php?id=3884

解法:反复使用扩展欧拉定理,递归求解。

https://www.cnblogs.com/SovietPower/p/8353374.html

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF  0x3f3f3f3f
#define mod 998244353
#define PI acos(-1)
using namespace std;
typedef long long ll ;

ll quickpow(ll a , ll b , ll mo)
{
    ll ans = 1;
    while(b)
    {
        if(b&1)
            ans = ans * a % mo ;
        b >>= 1 ;
        a = a * a % mo ;
    }
    return ans % mo ;
}

ll get_phi(ll n)
{
    ll res = n  ;
    for(int i = 2 ; i * i <= n ; i++)
    {
        if(n%i == 0)
        {
            res -= res / i ;
            while(n % i == 0)
            {
                n /= i;
            }
        }
    }
    if(n > 1) res -=  res / n ;
    return res ;
}
ll solve(ll n)
{
    if(n == 1) return 0 ;
    ll t = get_phi(n);
    return quickpow(2 , solve(t)+t , n);
}

int main()
{
    int t ;
    scanf("%d" , &t);
    while(t--)
    {
        ll p ;
        scanf("%lld" , &p);
        cout << solve(p) << endl;
    }
    return 0 ;
}

你可能感兴趣的:(扩展欧拉定理的应用)