当前位置:网站首页>快速幂---学习笔记
快速幂---学习笔记
2022-08-01 12:10:00 【不迷茫的小航】
暑假准备搞学习笔记,这是第一次,看起来的效果估计很差,之后的每日一题会一直写的,加油!
问题:
快速求出来a的k次方mod P的结果,在O(log k)的时间范围内求出结果
a,p,k数据范围都在1到10的九次方内
核心思路:反复平方法
预处理+二进制转换
首先预处理,
mod p,
mod p,一直到
mod p总共logk个数值
目标是把a的k次方看成是之前预处理的各个数字的乘积
关键还是把 k 二进制化, 将 k 进行二进制表示
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
int qmi(int a, int k, int p)
{
int res = 1;
while(k)
{
if(k & 1) res = (LL) res * a % p;//对k取地址,看最后一位,是1就乘上a
k >>= 1;//去掉最后一位,再进行判断
a = (LL) a * a % p;//a变为下一个,即最后的平方
}
return res;
}
int main()
{
int n;
scanf("%d", &n);
while(n -- )
{
int a, k, p;
scanf("%d%d%d", &a, &k, &p);
printf("%d\n", qmi(a,k,p));
}
return 0;
}

边栏推荐
猜你喜欢
随机推荐
What is MNIST (what does plist mean)
The use of Ts - Map type
故障007:dexp导数莫名中断
bpmn-process-designer基础上进行自定义样式(工具、元素、菜单)
pandas connects to the oracle database and pulls the data in the table into the dataframe, filters all the data from the current time (sysdate) to one hour ago (filters the range data of one hour)
ECCV22|只能11%的参数就能优于Swin,微软提出快速预训练蒸馏方法TinyViT
sql中ddl和dml(数据库表与视图的区别)
[Open class preview]: Research and application of super-resolution technology in the field of video quality enhancement
R语言ggplot2可视化:使用ggpubr包的ggdensity函数可视化密度图、使用stat_central_tendency函数在密度中添加均值竖线并自定义线条类型
(ES6 and above and TS) Map object to array
Alibaba Cloud Official Redis Development Specification
语音聊天app源码——语音聊天派对
(ES6以上以及TS) Map对象转数组
Data frame and remote frame of CAN communication
Istio Meetup China:全栈服务网格 - Aeraki 助你在 Istio 服务网格中管理任何七层流量
腾讯云原生:Areaki Mesh 在 2022 冬奥会视频直播应用中的服务网格实践
程序员的自我修养
CAN通信的数据帧和远程帧
每日一题:连续子数组的最大和(动态规划)
Deep understanding of Istio - advanced practice of cloud native service mesh









