算阶第一章a^b

题目描述

求 a 的 b 次方对 p 取模的值,其中 1≤a,b,p≤10^9

输入和输出

Input

三个用空格隔开的整数a,b和p。

Output

一个整数,表示a^b mod p的值。

样例

Sample Input

2 3 9

Sample Output

8
数据规模与约定
时间限制:1s1s
空间限制:256MB

#include <bits/stdc++.h>
using namespace std;
int power(int a,int b,int p)
{
    
    
	int ans=1%p;
	for(;b;b>>=1)
	{
    
    
		if(b&1) ans=(long long)ans*a%p;
		a=(long long)a*a%p;
	}
	return ans;
}
int main()
{
    
    
	long long x,y,z;
	cin>>x>>y>>z;
	cout<<power(x,y,z);
}

猜你喜欢

转载自blog.csdn.net/ydsrwex/article/details/112252971