#59【快速幂】A的B次方

版权声明:反正也没有人会转,下一个 https://blog.csdn.net/drtlstf/article/details/81710604

Description

给出三个整数 a,b,m,求 ab mod m 的值。

Input

一行三个整数 a,b,m。

Output

一个整数,表示 ab mod m 的值。

Sample Input

2 100 1007

Sample Output

169

HINT

 

对于全部数据,1≤a,b,m≤109​​。

第一次开int,WA了,后来机智的改成long long,A了。

#include <iostream>

using namespace std;

int main(void)
{
	long long a, b, c, s;
	
	while (~scanf("%d%d%d", &a, &b, &c))
	{
		s = 1;
		while (b != 1)
		{
			if (b & 1) // 判定奇数,位运算与更省时
			{
				s *= a;
				s %= c;
			}
			a *= a;
			a %= c;
			b >>= 1; // 就是除2,位运算右移省时
		}
		printf("%d\n", s * a % c);
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/drtlstf/article/details/81710604