当前位置:网站首页>数学知识——快速幂的理解及例题
数学知识——快速幂的理解及例题
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;
}
边栏推荐
- Flag bits in assembly language: CF, PF, AF, ZF, SF, TF, if, DF, of
- Orthogonal test method and function diagram method for test case design
- How to write a client-side technical solution
- 奠定少儿编程成为基础学科的原理
- Shenzhen will speed up the cultivation of ecology to build a global "Hongmeng Oula city", with a maximum subsidy of 10million yuan for excellent projects
- A summary of common interview questions in 2022, including 25 technology stacks, has helped me successfully get an offer from Tencent
- Markdown edit syntax
- Thinkphp內核工單系統源碼商業開源版 多用戶+多客服+短信+郵件通知
- 汇编语言中的标志位:CF、PF、AF、ZF、SF、TF、IF、DF、OF
- Starting from the classification of database, I understand the map database
猜你喜欢
What methods should service define?
Mysql表insert中文变?号的问题解决办法
Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知
Idea autoguide package and autodelete package Settings
Landing guide for "prohibit using select * as query field list"
unable to execute xxx. SH: operation not permitted
Summary of common string processing functions in C language
Why can't you remember when reading? Why can't you remember- My technology learning methodology
Deeply understand the concepts of synchronization and asynchrony, blocking and non blocking, parallel and serial
Cache consistency solution - how to ensure the consistency between the cache and the data in the database when changing data
随机推荐
Pytoch --- use pytoch to realize u-net semantic segmentation
C语言猜数字游戏
Learn BeanShell before you dare to say you know JMeter
Mapping location after kotlin confusion
Exposure X8 Standard Version picture post filter PS, LR and other software plug-ins
Vmware安装win10报错:operating system not found
DJB Hash
Introduction to Luogu 3 [circular structure] problem list solution
万卷共知,一书一页总关情,TVP读书会带你突围阅读迷障!
Let正版短信测压开源源码
Learn what definitelytyped is through the typescript development environment of SAP ui5
C language practice - number guessing game
unable to execute xxx. SH: operation not permitted
Shenzhen will speed up the cultivation of ecology to build a global "Hongmeng Oula city", with a maximum subsidy of 10million yuan for excellent projects
60后关机程序
Idea automatic package import and automatic package deletion settings
Oracle stored procedure and job task setting
Ruby replaces gem Alibaba image
cs架构下抓包的几种方法
Ten thousand volumes are known to all, and one page of a book is always relevant. TVP reading club will take you through the reading puzzle!