当前位置:网站首页>数学知识——快速幂的理解及例题
数学知识——快速幂的理解及例题
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;
}
边栏推荐
- 6月书讯 | 9本新书上市,阵容强大,闭眼入!
- Tawang food industry insight | current situation, consumption data and trend analysis of domestic infant complementary food market
- One click generation and conversion of markdown directory to word format
- Message mechanism -- message processing
- [improvement class] st table to solve the interval maximum value problem [2]
- Shutdown procedure after 60
- The solution to the complexity brought by lambda expression
- Playing with concurrency: what are the ways of communication between threads?
- CorelDRAW graphics suite2022 free graphic design software
- LM09丨费雪逆变换反转网格策略
猜你喜欢

idea自動導包和自動删包設置

Rhcsa --- work on the fourth day

Deeply understand the concepts of synchronization and asynchrony, blocking and non blocking, parallel and serial

Common errors of dmrman offline backup

Landing guide for "prohibit using select * as query field list"

Why can't you remember when reading? Why can't you remember- My technology learning methodology

CorelDRAW graphics suite2022 free graphic design software

CY7C68013A之keil编译代码

Let正版短信测压开源源码

2022-003arts: recursive routine of binary tree
随机推荐
Binary tree problem solving (1)
Research on the security of ognl and El expressions and memory horse
How muddy is the water in the medical beauty industry with a market scale of 100 billion?
Typescript function details
ThinkPHP kernel work order system source code commercial open source version multi user + multi customer service + SMS + email notification
One click generation and conversion of markdown directory to word format
Pytoch yolov5 runs bug solution from 0:
记录一次Unity 2020.3.31f1的bug
Starting from the classification of database, I understand the map database
6月书讯 | 9本新书上市,阵容强大,闭眼入!
Gin framework learning code
阿里云polkit pkexec 本地提权漏洞
[C language] Dynamic Planning --- from entry to standing up
There is no prompt for SQL in idea XML, and the dialect setting is useless.
Win10 disk management compressed volume cannot be started
Record the bug of unity 2020.3.31f1 once
Use of typescript classes
Let genuine SMS pressure measurement open source code
Tawang food industry insight | current situation, consumption data and trend analysis of domestic infant complementary food market
Pytorch---使用Pytorch进行鸟类的预测