当前位置:网站首页>数学知识——快速幂的理解及例题
数学知识——快速幂的理解及例题
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;
}
边栏推荐
- Geotrust OV Multi - Domain Domain SSL Certificate rmb2100 per year contains several Domain names?
- Federal learning: dividing non IID samples according to Dirichlet distribution
- What are the rules and trading hours of agricultural futures contracts? How much is the handling fee deposit?
- Deep understanding of lambda expressions
- Mysql中常见的锁
- Shutdown procedure after 60
- Acelems Expressway microgrid energy efficiency management platform and intelligent lighting solution intelligent lighting tunnel
- Learn what definitelytyped is through the typescript development environment of SAP ui5
- Unit testing classic three questions: what, why, and how?
- Thinkphp Kernel wo system source Commercial Open source multi - user + multi - Customer Service + SMS + email notification
猜你喜欢
win10 磁盘管理 压缩卷 无法启动问题
LeetCode-归并排序链表
Three years of experience in Android development interview (I regret that I didn't get n+1, Android bottom development tutorial
Record the bug of unity 2020.3.31f1 once
Getting started with pytest -- description of fixture parameters
Starting from the classification of database, I understand the map database
Leetcode merge sort linked list
解决:代理抛出异常错误
Landing guide for "prohibit using select * as query field list"
Learn what definitelytyped is through the typescript development environment of SAP ui5
随机推荐
win10 磁盘管理 压缩卷 无法启动问题
Deeply understand the concepts of synchronization and asynchrony, blocking and non blocking, parallel and serial
VMware installation win10 reports an error: operating system not found
How muddy is the water in the medical beauty industry with a market scale of 100 billion?
Read "the way to clean code" - function names should express their behavior
C language practice - binary search (half search)
汇编语言中的标志位:CF、PF、AF、ZF、SF、TF、IF、DF、OF
Virtual machine installation deepin system
Design and implementation of general interface open platform - (44) log processing of API services
Yyds dry goods inventory kubernetes introduction foundation pod concept and related operations
[C language] Dynamic Planning --- from entry to standing up
oracle 存储过程与job任务设置
Pytorch-Yolov5从0运行Bug解决:
TypeScript类的使用
Let正版短信测压开源源码
Online incremental migration of DM database
6.30年终小结,学生时代结束
Binary tree problem solving (2)
Cannot activate CONDA virtual environment in vscode
Pit encountered in win11 pytorch GPU installation