牛客小白月赛12 -- B 华华教月月做数学

链接:https://ac.nowcoder.com/acm/contest/392/B
来源:牛客网
 

题目描述

找到了心仪的小姐姐月月后,华华很高兴的和她聊着天。然而月月的作业很多,不能继续陪华华聊天了。华华为了尽快和月月继续聊天,就提出帮她做一部分作业。
月月的其中一项作业是:给定正整数A、B、P,求ABmodPABmodP的值。华华觉得这实在是毫无意义,所以决定写一个程序来做。但是华华并不会写程序,所以这个任务就交给你了。
因为月月的作业很多,所以有T组询问。

输入描述:

第一行一个正整数T表示测试数据组数。
接下来T行,每行三个正整数A、B、P,含义如上文。

输出描述:

输出T行,每行一个非负整数表示答案。

示例1

输入

复制

2
2 5 10
57284938291657 827493857294857 384729583748273

输出

复制

2
18924650048745

备注:

1≤T≤1031≤T≤103,1≤A,B,P≤10181≤A,B,P≤1018

快速幂+ 快乘模板 

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std ;
typedef long long LL;
const LL mod = 1e9+7 ;
const int MAX = 100055 ;
const int inf = 0x3f3f3f3f ;
LL qmul(LL a , LL b ,LL p ){ // 快乘
	LL ans = 0 ;
	while(b){
		if(b&1){
			ans = (ans+a )%p ;
		}
		a = a%p*2%p ;
		b>>=1 ;
	}
	return ans%p  ;
}
LL qpow(LL a ,LL b ,LL mod){ // 快速幂
    LL ans = 1 ;
    while(b){
    	if(b&1){
			ans = qmul(ans,a,mod)%mod;
		}
		a = qmul(a,a,mod)%mod ;
		b>>= 1 ;
	}
	return ans  ;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL),cout.tie(NULL);
    LL T ;
    cin >>T ;
    while(T--){
        LL a , b , p ;
        cin >>a >> b >>  p ;
        cout<<qpow(a,b,p) <<endl ;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41661809/article/details/88376515