快速幂
题目链接

题目描述

输入b,p,k的值,求b^p mod k的值。其中b,p,k*k为长整型数。

输入输出格式

洛谷 | P1226 【快速幂】

时空限制

  • 时间:1000ms
  • 空间:128MB

代码

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;

//求 a^b % m,迭代写法
LL binaryPow(LL a, LL b, LL m){
	LL ans = 1;
	while(b > 0){
		if(b & 1){  //位运算,比模2运算快 
			ans  = ans*a%m;
		}
		a = a*a%m;
		b >>= 1;
	}
	return ans;
} 
int main(){
	LL b,p,k;
	scanf("%lld%lld%lld",&b,&p,&k);
	LL result = binaryPow(b,p,k);
	printf("%lld^%lld mod %lld=%lld\n",b,p,k,result);
	return 0;
} 

:最后一个测试点过不了。

相关文章:

  • 2021-06-15
  • 2021-11-17
  • 2021-12-01
  • 2022-01-20
  • 2021-09-10
  • 2021-12-07
猜你喜欢
  • 2022-02-14
  • 2022-03-03
  • 2021-06-07
  • 2022-03-04
  • 2021-11-01
  • 2021-09-30
相关资源
相似解决方案