版权声明:个人水平有限,表述不当或疏漏之处欢迎读者提问交流,共同进步! 作者:J__Max 非商业转载请注明出处,商业转载请联系作者获得授权。 https://blog.csdn.net/J__Max/article/details/83338513
codevs 2022 复仇 快速幂模版题
-
直接套快速幂的模版即可,变量类型开 long long int 防止上溢出
-
代码如下:
//
// main.cpp
// progress
//
// Created by J__Max on 2018/10/19.
// Copyright © 2018年 J__Max. All rights reserved.
//
#include <iostream>
using namespace std;
//快速幂
void solve(int a, int b, int p){
long long int ans = 1, base = a;
while (b != 0) {
if((b & 1) != 0){
ans *= base;
ans %= p;
}
base *= base;
base %= p;
b >>= 1;
}
cout<<ans<<endl;
}
int main(int argc, const char * argv[]) {
int a, b, p;
cin>>a>>b>>p;
solve(a, b, p);
return 0;
}