当前位置:网站首页>数学知识:快速幂—快速幂
数学知识:快速幂—快速幂
2022-06-23 07:01:00 【奋斗吧!骚年!】
题目:AcWing 875. 快速幂
给定 n 组 ai,bi,pi,对于每组数据,求出 aibimod pi 的值。
输入格式
第一行包含整数 n。
接下来 n 行,每行包含三个整数 ai,bi,pi。
输出格式
对于每组数据,输出一个结果,表示 aibimod pi 的值。
每个结果占一行。
数据范围
1≤n≤100000,
1≤ai,bi,pi≤2×109
输入样例:
2
3 2 5
4 3 9
输出样例:
4
1
题目分析:
求 aibimod pi
先预处理出:
a的20次方 mod p
a的21次方 mod p
…
a的2logk次方 mod p
然后将bi次方转化为x20+x21+…x2logk
这是运用二进制的性质
#include <iostream>
using namespace std;
const int N = 100010;
typedef long long LL;
LL qmi(int a,int k,int p)
{
LL res=1%p;
while(k)
{
if(k&1)res=(LL)res*a%p;
a=(LL)a*a%p;
k>>=1;
}
return res;
}
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
int a,k,p;
scanf("%d%d%d",&a,&k,&p);
cout<<qmi(a,k,p)<<endl;
}
return 0;
}
边栏推荐
- HCIP之路MPLS
- Redis设置密码
- char和varchar区别
- NFS 特别注意权限的问题
- MySQL获取系统时间段
- Live broadcast review | how can the container transformation of traditional applications be fast and stable?
- EXCEL VBA 入门与实用例子
- Qt工程报错:-1: error: Cannot run compiler ‘clang++‘. Output:mingw32-make.exe
- 在线文本过滤小于指定长度工具
- Intelligence Education - how to merge codes when code conflicts occur in multi person collaborative development?
猜你喜欢

HCIP之路

Intelligence Education - how to merge codes when code conflicts occur in multi person collaborative development?

干货来了|《PaaS》合辑抢先看~

《一周的朋友》

Wechat multiplayer chat and Roulette Games (websocket Implementation)

Yan's DP analysis

Both are hard disk partitions. What is the difference between C disk and D disk?
![[* * * array * * *]](/img/fe/2520d85faab7d1fbf5036973ff5965.png)
[* * * array * * *]

EXCEL VBA 入门与实用例子

Eureka service registration and discovery
随机推荐
HCIP之路
1278_FreeRTOS_借助prvAddCurrentTaskToDelayedList接口理解delayed task
【markdown】markdown 教程大归纳
Spock constraint - call frequency / target / method parameters
Abnormal logic reasoning problem of Huawei software test written test
20bn Jester complete dataset Download
Download the OSS file and modify the file name
Wechat multiplayer chat and Roulette Games (websocket Implementation)
MySQL (11) - sorting out MySQL interview questions
微信多人聊天及轮盘小游戏(websocket实现)
Online text filter less than specified length tool
20BN-Jester完整数据集下载
[interface automation] software testing the core skills of salary increase to increase salary by 200%
JS to determine the added and decreased elements of two arrays
HCIP之路MPLS
2.概率论-概率论公理
[pit stepping record] a pit where the database connection is not closed and resources are released
Yolov5 detecting small targets (with source code)
csrf攻击在laravel中如何解决
Spock sub piling