当前位置:网站首页>数学知识——快速幂的理解及例题
数学知识——快速幂的理解及例题
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;
}
边栏推荐
- C language guessing numbers game
- Why can't you remember when reading? Why can't you remember- My technology learning methodology
- 万卷共知,一书一页总关情,TVP读书会带你突围阅读迷障!
- DC-1靶场搭建及渗透实战详细过程(DC靶场系列)
- Pit encountered in win11 pytorch GPU installation
- Virtual machine installation deepin system
- idea自动导包和自动删包设置
- Rhcsa --- work on the third day
- Shutdown procedure after 60
- CorelDRAW graphics suite2022 free graphic design software
猜你喜欢

Realize the function of data uploading

Several methods of capturing packets under CS framework

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

Ognl和EL表达式以及内存马的安全研究

奠定少儿编程成为基础学科的原理

win10 磁盘管理 压缩卷 无法启动问题

A summary of common interview questions in 2022, including 25 technology stacks, has helped me successfully get an offer from Tencent

Alibaba cloud polkit pkexec local rights lifting vulnerability

Idea autoguide package and autodelete package Settings

VMware installation win10 reports an error: operating system not found
随机推荐
win10 磁盘管理 压缩卷 无法启动问题
Shutdown procedure after 60
Geotrust OV Multi - Domain Domain SSL Certificate rmb2100 per year contains several Domain names?
Websites that it people often visit
Mysql中常见的锁
The core idea of performance optimization, dry goods sharing
阿里云polkit pkexec 本地提权漏洞
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!
C语言猜数字游戏
Hcip day 17
How do I interview for a successful software testing position? If you want to get a high salary, you must see the offer
二叉树解题(一)
What are the rules and trading hours of agricultural futures contracts? How much is the handling fee deposit?
Leetcode merge sort linked list
Interview question: do you know the difference between deep copy and shallow copy? What is a reference copy?
[improvement class] st table to solve the interval maximum value problem [2]
Unity particle Foundation
2022-003arts: recursive routine of binary tree
面试会问的 Promise.all()
ThinkPHP kernel work order system source code commercial open source version multi user + multi customer service + SMS + email notification