当前位置:网站首页>数学知识——快速幂的理解及例题
数学知识——快速幂的理解及例题
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;
}
边栏推荐
- 60后关机程序
- win11安装pytorch-gpu遇到的坑
- 洛谷入门3【循环结构】题单题解
- My first experience of shadowless cloud computer
- Ognl和EL表达式以及内存马的安全研究
- win10 磁盘管理 压缩卷 无法启动问题
- Idea automatic package import and automatic package deletion settings
- Idea autoguide package and autodelete package Settings
- Geotrust OV Multi - Domain Domain SSL Certificate rmb2100 per year contains several Domain names?
- oracle 存储过程与job任务设置
猜你喜欢

Websites that it people often visit

A summary of common interview questions in 2022, including 25 technology stacks, has helped me successfully get an offer from Tencent
![[C language] basic learning notes](/img/d2/1aeb2d37d97b9cfe4b21aa3ac37645.png)
[C language] basic learning notes

Unit testing classic three questions: what, why, and how?

Summary of main account information of zhengdaliu 4

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

Idea automatic package import and automatic package deletion settings

Deep understanding of lambda expressions

How to write a client-side technical solution

Lm09 Fisher inverse transform inversion mesh strategy
随机推荐
洛谷入门3【循环结构】题单题解
Cache consistency solution - how to ensure the consistency between the cache and the data in the database when changing data
Analyze the space occupied by the table according to segments, clusters and pages
One click generation and conversion of markdown directory to word format
Online incremental migration of DM database
A summary of common interview questions in 2022, including 25 technology stacks, has helped me successfully get an offer from Tencent
Promise all()
ThinkPHP kernel work order system source code commercial open source version multi user + multi customer service + SMS + email notification
C language guessing numbers game
Free drawing software recommended - draw io
Major domestic quantitative trading platforms
Alibaba cloud polkit pkexec local rights lifting vulnerability
Interview question: do you know the difference between deep copy and shallow copy? What is a reference copy?
Mysql中常见的锁
Exposure X8标准版图片后期滤镜PS、LR等软件的插件
Leetcode- insert and sort the linked list
千亿市场规模医疗美容行业的水究竟有多浑?
解决:代理抛出异常错误
万卷共知,一书一页总关情,TVP读书会带你突围阅读迷障!
Learn what definitelytyped is through the typescript development environment of SAP ui5