蓝桥杯 ADV-204 试题 算法提高 快速幂

资源限制

时间限制:1.0s   内存限制:256.0MB

问题描述

  给定A, B, P,求(A^B) mod P。

输入格式

  输入共一行。
  第一行有三个数,N, M, P。

输出格式

  输出共一行,表示所求。

样例输入

2 5 3

样例输出

扫描二维码关注公众号,回复: 10795264 查看本文章

2

数据规模和约定

  共10组数据
  对100%的数据,A, B为long long范围内的非负整数,P为int内的非负整数。

 分析:常规题目,直接套用快速幂的算法就行,看代码

#include <iostream>
#include <cmath>
typedef long long ll;
using namespace std;
ll f(ll a,ll b ,ll d)
{
	if(b==0) return 1;
	if(b&1) return f(a,b-1,d)*(a%d)%d;
	ll temp=f(a,b/2,d);
	return temp*temp%d;
}
int main()
{
	ll A,B,P;
	cin>>A>>B>>P;
	cout<<f(A,B,P);
	return 0;
 } 
发布了80 篇原创文章 · 获赞 3 · 访问量 922

猜你喜欢

转载自blog.csdn.net/qq_40570410/article/details/104789425