A^B Mod C

给出3个正整数A B C,求A^B Mod C。
例如,3 5 8,3^5 Mod 8 = 3。
Input
3个正整数A B C,中间用空格分隔。(1 <= A,B,C <= 10^9)
Output
输出计算结果
Sample Input
3 5 8
Sample Output
3
最基本的快速幂

#include <stdio.h>

typedef long long ll;
int pow_mod(ll a, ll n, ll MOD) {
    ll res = 1;
    while (n) {
        if(n&1) res = res * a % MOD;
        a = a * a % MOD;
        n >>= 1;
    }
    return res ;
}

int main()
{
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    printf("%d\n", pow_mod(a, b, c));
    return 0;
}

猜你喜欢

转载自blog.csdn.net/hqzzbh/article/details/81118150