[C++][算法基础]求a的b次方模p的值(快速幂)

给定 n 组 ,对于每组数据,求出 的值。

输入格式

第一行包含整数 n。

接下来 n 行,每行包含三个整数 。

输出格式

对于每组数据,输出一个结果,表示 的值。

每个结果占一行。

数据范围

1≤n≤100000,

1≤≤2×

输入样例:
复制代码
2
3 2 5
4 3 9
输出样例:
复制代码
4
1

代码:

cpp 复制代码
#include<iostream>
#include<algorithm>
using namespace std;

long long a,b;
int n,p;
const int N = 100010;

long long QuickMul(long long a,long long b,int p){
    long long res = 1;
    while(b != 0){
        if((b & 1) == 1){
            res = res * a % p;
        }
        b = b / 2;
        a = a * a % p;
    }
    return res;
}

int main(){
    cin>>n;
    while(n--){
        cin>>a>>b>>p;
        long long ans = QuickMul(a,b,p);
        cout<<ans<<endl;
    }
    return 0;
}
相关推荐
FlightYe几秒前
音视频修炼之容器与协议(一):MP4和MOV的box剖析
android·linux·c++·ffmpeg·音视频
音草28 分钟前
AVL 树详解:从概念到 C++ 实现
c++
凉茶钱39 分钟前
list:哨兵节点,const_iterator 和 const iterator的区别与迭代器失效
数据结构·c++·list
沫璃染墨1 小时前
从零入门计算机网络系列(六):TCP套接字编程实战——从listen监听到客户端与服务器通信
linux·服务器·c++·tcp/ip·计算机网络·tcp
Jsundoku1 小时前
第六章 机器学习 -- 支持向量机
算法·机器学习·支持向量机
?abc!1 小时前
(数组)448. 找到所有数组中消失的数字
算法
淡海水1 小时前
05-03-源码-Boehm GC分配与碎片管理
开发语言·算法·unity·c#·游戏引擎·gc
努力实干1 小时前
C++ 面向对象与工程架构
开发语言·c++·架构
代钦塔拉2 小时前
【C++/Qt 进阶】设计模式系列(6):桥接模式——把“两个独立变化的维度“拆开
c++·qt·设计模式