快速幂 (欧拉降幂)代码实现

P1226 【模板】快速幂||取余运算

#include<bits/stdc++.h>
#define LL long long
using namespace std;

LL quick_pow(int a, int b, int p){
    LL res = 1 % p;
    while(b){
        if(b & 1) res = res * a % p;
        a = a * (LL)a % p;
        b >>= 1; 
    } 
    return res;
}

int main(){
    int a, b, p;
    cin >> a >> b >> p;
    cout << a << "^" << b << " mod " << p << "=";
    cout << quick_pow(a, b, p);
}

代码非常的简短!

 

posted @ 2021-11-30 23:58  Altwilio  阅读(51)  评论(1编辑  收藏  举报