当前位置:网站首页>数学知识——快速幂的理解及例题
数学知识——快速幂的理解及例题
2022-07-02 04:45:00 【吴雨4】
快速幂:快速的求出来a^k mod p的结果。
1<=a,k,p<=1e9。
快速幂其实就是预处理出来:

于是a^k可以表示为(二进制表示):

说明一下:出来a^(2^0)=a以外,其余的预处理一直都在平方。

题面:

代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
int qmi(int a,int k,int p)
{
int res=1;
while(k)
{
if(k&1) res=(ll)res*a%p;
k>>=1;
a=(ll) a*a%p;
}
return res;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
while(n--)
{
int a,k,p;
cin>>a>>k>>p;
cout<<qmi(a,k,p)<<endl;
}
return 0;
}
边栏推荐
- Use of typescript classes
- Flag bits in assembly language: CF, PF, AF, ZF, SF, TF, if, DF, of
- What are the rules and trading hours of agricultural futures contracts? How much is the handling fee deposit?
- Tawang food industry insight | current situation, consumption data and trend analysis of domestic infant complementary food market
- Binary tree problem solving (2)
- Hcip day 17
- 【毕业季·进击的技术er】年少有梦,何惧彷徨
- MySQL table insert Chinese change? Solution to the problem of No
- [graduation season · advanced technology Er] young people have dreams, why are they afraid of hesitation
- 正大留4的主账户信息汇总
猜你喜欢

培养中小学生对教育机器人的热爱之心

MySQL table insert Chinese change? Solution to the problem of No

CY7C68013A之keil编译代码

idea自动导包和自动删包设置

What data does the main account of Zhengda Meiou 4 pay attention to?

Pytorch---使用Pytorch进行图像定位

Tawang food industry insight | current situation, consumption data and trend analysis of domestic infant complementary food market

How do I interview for a successful software testing position? If you want to get a high salary, you must see the offer

Interview question: do you know the difference between deep copy and shallow copy? What is a reference copy?

Exposure X8 Standard Version picture post filter PS, LR and other software plug-ins
随机推荐
【提高课】ST表解决区间最值问题【2】
Pytoch --- use pytoch for image positioning
Hcip day 17
Mysql中常见的锁
My first experience of shadowless cloud computer
Homework of the 16th week
AcrelEMS高速公路微电网能效管理平台与智能照明解决方案智慧点亮隧道
Pytoch --- use pytoch to realize u-net semantic segmentation
Interview question: do you know the difference between deep copy and shallow copy? What is a reference copy?
Why can't you remember when reading? Why can't you remember- My technology learning methodology
The solution to the complexity brought by lambda expression
Yyds dry goods inventory kubernetes introduction foundation pod concept and related operations
win11安装pytorch-gpu遇到的坑
Getting started with pytest -- description of fixture parameters
June book news | 9 new books are listed, with a strong lineup and eyes closed!
Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
Tawang food industry insight | current situation, consumption data and trend analysis of domestic infant complementary food market
Exposure X8标准版图片后期滤镜PS、LR等软件的插件
面试会问的 Promise.all()
Markdown edit syntax