51Nod 1046-A^B Mod C

基准时间限制:1 秒 空间限制:131072 KB 分值: 0  难度:基础题
 收藏
 关注
给出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
输出计算结果
Input示例
3 5 8
Output示例
3
题解:A*B%MOD=(A%MOD*B%MOD)%MOD
#include<stdio.h>
#include<math.h>
#define ll long long int
ll a,b,c,sum=1;
void f(ll x,ll y,ll mod)
{
	while(y)
	{
		if(y&1)
		{
			sum=(sum%mod*x%mod)%mod;
		}
		x=(x%mod*x%mod)%mod;
		y>>=1;
	}
}
int main()
{
	scanf("%lld%lld%lld",&a,&b,&c);
	f(a,b,c);
	printf("%lld",sum);
	return 0;
} 


猜你喜欢

转载自blog.csdn.net/qq_40727946/article/details/80270606
今日推荐