首页 > 其他 > 详细

a^b

时间:2018-07-16 16:54:08      阅读:279      评论:0      收藏:0      [点我收藏+]

题目描述

求 a 的 b 次方对 p 取模的值,其中 1≤a,b,p≤10^9

输入

三个用空格隔开的整数a,b和p。

输出

一个整数,表示a^b mod p的值。

样例输入

2 3 9

样例输出

8

技术分享图片
#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#define range(i,a,b) for(int i=a;i<=b;++i)
#define LL long long
#define rerange(i,a,b) for(int i=a;i>=b;--i)
#define fill(arr,tmp) memset(arr,tmp,sizeof(arr))
using namespace std;
LL a,b,p;
void init(){
    cin>>a>>b>>p;
}
LL qpow(LL a,LL b){
    LL ans=1;a%=p;
    while(b){
        if(b&1)ans=ans*a%p;
        a=a*a%p;
        b>>=1;
    }
    return ans;
}
void solve(){
    cout<<qpow(a,b)%p<<endl;
}
int main() {
    init();
    solve();
    return 0;
}
View Code

 

a^b

原文:https://www.cnblogs.com/Rhythm-/p/9318474.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!