A^B Mod C 51Nod - 1046(快速幂)

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;
ll pow_mod(ll a,ll b,ll mod)
{   ll res;
    res=1;
    while(b)
    {
        if(b&1)
            res=res*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return res; 
}
int main()
{
    ll  p,a,t,n;
    scanf("%lld%lld%lld",&a,&p,&n);
    t=pow_mod(a,p,n);
    printf("%lld\n",t);
    return  0;
}

题解:模板,注意数据大小。防止爆int最好用long long。

猜你喜欢

转载自blog.csdn.net/shf1730797676/article/details/81982429
今日推荐