快速幂

我好菜我这个背了好几遍了还是背不住妈的

#include <bits/stdc++.h>

#define Space putchar(' ')
#define Enter puts("")

using namespace std;

typedef long long ll;
typedef double db;

inline ll Read()
{
    ll ans = 0;
    char ch = getchar() , las = ' ';
    while(!isdigit(ch))
    {
        las = ch;
        ch = getchar();
    }
    while(isdigit(ch))
    {
        ans = (ans << 3) + (ans << 1) + ch - '0';
        ch = getchar();
    }
    if(las == '-')
        ans = -ans;
    return ans;
}

inline void Write(ll x)
{
    if(x < 0)
    {
        x = -x;
        putchar('-');
    }
    if(x >= 10)
        Write(x / 10);
    putchar(x % 10 + '0');
}

inline ll Quick_Power_Mod(ll a , ll b , ll Mod) 
{
    ll Ans = 1, Base = a;
    while(b != 0) 
    {
        if(b & 1 != 0) 
            Ans *= Base;
        Base *= Base % Mod;
        b >>= 1;
    }
    return Ans;
}

inline ll Quick_Power(ll a , ll b)
{
    ll Ans = 1 , Base = a;
    while(b != 0)
    {
        if(b & 1 != 0)
            Ans *= Base;
        Base *= Base;
        b >>= 1;
    }
    return Ans;
}

int main()
{
    ll Base , Point , Mod;
    Base = Read();
    Point = Read();
    Mod = Read();
    ll Result_Mod = Quick_Power_Mod(Base , Point , Mod);
    ll Result = Quick_Power(Base , Point);
    Write(Result_Mod);
    Enter;
    Write(Result);
    return 0;
}

 

posted @ 2020-11-24 19:20  Tenderfoot  阅读(62)  评论(0编辑  收藏  举报