当前位置:网站首页>数学知识:快速幂—快速幂
数学知识:快速幂—快速幂
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;
}
边栏推荐
- To conquer salt fields and vegetable fields with AI, scientific and technological innovation should also step on the "field"
- 小爱音箱连接网络异常解决办法
- Nacos adapts Oracle11g create table DDL statement
- Ffplay realizes user-defined input stream playback
- MySQL (V) - locks and transactions
- Here comes the dry goods | PAAS collection to see first ~
- GIF验证码分析
- The original cloud landed in deep water, and the cloud product family of Boyun container released four values
- HCIP之路MPLS
- How bootstrap clears floating styles
猜你喜欢

如何优雅的快速下载谷歌云盘的大文件 (二)
![[game theory] basic knowledge](/img/eb/08b1ce5106e574dc42be58f72fbab9.jpg)
[game theory] basic knowledge

《一周的朋友》

MySQL Niuke brush questions

MySQL (VIII) - explain

Simpledateformat thread safety issues

What is customer experience automation?

Product axure9 (English version), prototype design background dynamic secondary menu display content
![[Planet selection] how to efficiently build fine-grained two-way links between roam and thebrain?](/img/ee/ce9f55694b28c391eb07cb11298caf.jpg)
[Planet selection] how to efficiently build fine-grained two-way links between roam and thebrain?

Data types in tensorflow
随机推荐
Eureka服务注册与发现
Focusing on the industry, enabling customers | release of solutions for the five industries of the cloud container cloud product family
Console Application
【云计算赛项】职业技能竞赛--容器开发部分例题Pig快速开发框架
Realization of rolling broadcast effect
How bootstrap clears floating styles
[2022 graduation season] from graduation to transition to the workplace
《一周的朋友》
[pit stepping record] a pit where the database connection is not closed and resources are released
[interface automation] software testing the core skills of salary increase to increase salary by 200%
在线JSON转CSharp(C#)Class工具
传智教育 | 项目发布前如何打tag标签及标签命名规范
U-Net: Convolutional Networks for Biomedical Image Segmentation
Left multiply right multiply matrix problem
Matlab随机波动率SV、GARCH用MCMC马尔可夫链蒙特卡罗方法分析汇率时间序列
3DMAX plug-in development environment configuration and fileexport and utilities template testing
csrf攻击在laravel中如何解决
RFID data security experiment: C # visual realization of parity check, CRC redundancy check and Hamming code check
Nacos adapts to oracle11g- modify the source code of Nacos
Can you think of a better way to solve the problem of string inversion?