当前位置:网站首页>数学知识:快速幂—快速幂
数学知识:快速幂—快速幂
2022-06-23 07:01:00 【奋斗吧!骚年!】
题目:AcWing 875. 快速幂
给定 n 组 ai,bi,pi,对于每组数据,求出 aibimod pi 的值。
输入格式
第一行包含整数 n。
接下来 n 行,每行包含三个整数 ai,bi,pi。
输出格式
对于每组数据,输出一个结果,表示 aibimod pi 的值。
每个结果占一行。
数据范围
1≤n≤100000,
1≤ai,bi,pi≤2×109
输入样例:
2
3 2 5
4 3 9
输出样例:
4
1
题目分析:
求 aibimod pi
先预处理出:
a的20次方 mod p
a的21次方 mod p
…
a的2logk次方 mod p
然后将bi次方转化为x20+x21+…x2logk
这是运用二进制的性质
#include <iostream>
using namespace std;
const int N = 100010;
typedef long long LL;
LL qmi(int a,int k,int p)
{
LL res=1%p;
while(k)
{
if(k&1)res=(LL)res*a%p;
a=(LL)a*a%p;
k>>=1;
}
return res;
}
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
int a,k,p;
scanf("%d%d%d",&a,&k,&p);
cout<<qmi(a,k,p)<<endl;
}
return 0;
}
边栏推荐
- Matlab随机波动率SV、GARCH用MCMC马尔可夫链蒙特卡罗方法分析汇率时间序列
- 论文写作之WPS安装Mathtype插件编写数学公式
- Eureka服务注册与发现
- 分布式ID生成
- Spock sub piling
- Data types in tensorflow
- [Laoke] how should ordinary people learn technology?
- [深度学习][原创]如何不用yolov5权重或者模型进行目标检测和绘制map等参数图
- [pit stepping record] a pit where the database connection is not closed and resources are released
- G++ compilation command use
猜你喜欢

MYSQL牛客刷题

Sstable details

Qt 使用QDomDocument读取xml文件

Online JSON to CSharp (c) class tool
![[game theory] basic knowledge](/img/eb/08b1ce5106e574dc42be58f72fbab9.jpg)
[game theory] basic knowledge

GIF验证码分析

Tp6+redis+think-queue+supervisor implements the process resident message queue /job task

What is customer experience automation?

Live broadcast review | how can the container transformation of traditional applications be fast and stable?

1278_FreeRTOS_借助prvAddCurrentTaskToDelayedList接口理解delayed task
随机推荐
一篇文章学会er图绘制
Decoding and practice of cmaf Technology
'Latin-1' codec can't encode characters in position 103-115: body ('string of Chinese ') is not valid Latin-1
Left multiply right multiply matrix problem
Yan's DP analysis
char和varchar区别
WPS for thesis writing installs MathType plug-in to write mathematical formulas
[AI practice] xgb Xgbregression multioutputregressor parameter 1
分布式ID生成
Matlab随机波动率SV、GARCH用MCMC马尔可夫链蒙特卡罗方法分析汇率时间序列
The original cloud landed in deep water, and the cloud product family of Boyun container released four values
Cirium has gradually become the standard for airlines' carbon dioxide emission reporting
电脑如何安装MySQL?
How flannel works
基于51单片机的温度检测监测报警系统设计
Minio single node deployment Minio distributed deployment fool deployment process (I)
测试apk-异常管控NetTraffic攻击者开发
20bn Jester complete dataset Download
《一周的朋友》
Operation on a bit of binary