codevs 2022 復仇 快速冪模版題
阿新 • • 發佈:2018-11-04
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;
}