【扩展欧拉定理(欧拉降幂)模板题】power oj 2366

power oj 2366

思路如下:
【扩展欧拉定理(欧拉降幂)模板题】power oj 2366_第1张图片
(PS:果然手写比打出来那些东西舒服多了)

Code

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f
#define MID l + r >> 1
#define lsn rt << 1
#define rsn rt << 1 | 1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define lowbit(x) x & (-x)

using namespace std;
typedef long long ll;
typedef vector<int>:: iterator VITer;
const int maxV = 1e3 + 5;
const int maxE = 2e3 + 5;
const ll P = 1000000007;

bool Is_Prime(ll x)
{
    if ( x == 2)
        return true;
    for(ll i = 2 ; i * i <= x ; i ++)
    {
        if( x % i == 0)
            return false;
    }
    return true;
}
set<ll>st;
void prime_factor(ll x)
{
    for( ll i = 2; i <= x ; i ++)
    {
        if(Is_Prime(x))
        {
            st.insert(x);
            return ;
        }
        while(x % i == 0)
        {
            st.insert(i);
            x /= i;
        }
    }
}

ll Euler(ll x)
{
    if(x == 1) return 1;
    ll up = x, down = 1;
    prime_factor(x);
    while(!st.empty())
    {
        ll tmp = * st.begin();
        st.erase(st.begin());
        up *= (tmp - 1);
        down *= tmp;
    }
    return up / down;
}

ll fast_power(ll x, ll  y, ll p)
{
    ll base = x % p, ans = 1;
    while(y)
    {
        if(y & 1)
            ans =(ans % p) * (base % p) % p;
        base = (base % p) * (base % p) % p;
        y >>= 1;
    }
    return ans % p;
}

ll exEuler(ll x, ll y, ll p)//x^y
{
    return fast_power(x % p, y % Euler(p), p) % p;
}

int main()
{
    ll a, b, c;
    while(~scanf("%lld%lld%lld", &a, &b, &c))
    {
        printf("%lld\n", exEuler(a, exEuler(b, c, Euler(P)), P));
    }
    return 0;
}

你可能感兴趣的:(数论)