Untitled
raw download clone
C
views 12
,
size 568 b
12219
#include <stdio.h>
#define LL long long


LL fast_power(LL x, LL y, LL p);


int main(void)
{
    LL a, b, m;
    scanf("%lld%lld%lld", &a, &b, &m);
    printf("%lld\n", fast_power(a, b, m));
    return 0;
}

LL fast_power(LL x, LL y, LL p)
{
    LL res = 1;

    x = x % p;

    if (y == 0) return 1 % p;
    if (x == 0) return 0;

    while (y > 0)
    {
        if (y % 2 == 1)
        {
            res = (res % p) * (x % p) % p;
        }

        y = y / 2;
        x = (x % p) * (x % p) % p;
    }
    return res;
}
close fullscreen
Login or Register to edit or fork this paste. It's free.