蓝桥杯 算法提高 快速幂 快速幂longlong 下最稳的姿势

来源:互联网 发布:苏州软件测试 编辑:程序博客网 时间:2024/04/29 09:50

问题描述
  给定A, B, P,求(A^B) mod P。
输入格式
  输入共一行。
  第一行有三个数,N, M, P。
输出格式
  输出共一行,表示所求。
样例输入
2 5 3
样例输出
2
数据规模和约定
  共10组数据
  对100%的数据,A, B为long long范围内的非负整数,P为int内的非负整数。

http://lx.lanqiao.cn/problem.page?gpid=T375

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define ll long longusing namespace std;typedef long long LL;long long Max;LL fun(LL x,LL n){    LL res=1;    while(n>0)    {        x%=Max;        if(n & 1)            res=((res*x)%Max+Max)%Max;        x=(x*x)%Max;        n >>= 1;    }    return res;}int main(){    long long a,b;    cin>>a>>b>>Max;    cout<<fun(a,b)<<endl;}
0 0
原创粉丝点击