当前位置:网站首页>快速幂,逆元的求解
快速幂,逆元的求解
2022-08-11 07:49:00 【疯疯癫癫才自由】
875. 快速幂
给定 n
组 ai,bi,pi,对于每组数据,求出 abiimodpi
的值。
输入格式
第一行包含整数 n
。
接下来 n
行,每行包含三个整数 ai,bi,pi
。
输出格式
对于每组数据,输出一个结果,表示 abiimodpi
的值。
每个结果占一行。
数据范围
1≤n≤100000
,
1≤ai,bi,pi≤2×109
输入样例:
2
3 2 5
4 3 9
输出样例:
4
1
1)迭代写法求快速幂
/**
* 1)迭代写法求快速幂
*/
#include <iostream>
using namespace std;
typedef long long LL;
int qmi(int a,int b,int p)
{
int res=1;
while(b)
{
if(b&1)
res=res*(LL)a%p;
b >>=1;
a=(LL)a*a%p;
}
return res;
}
int main()
{
int n;
cin >> n;
while (n -- )
{
int a,b,p;
cin >> a >> b >> p;
cout << qmi(a,b,p) << endl;
}
return 0;
}
2)递归求快速幂
/**
* 2)递归求快速幂
*/
#include <iostream>
using namespace std;
typedef long long LL;
int qmi(int a,int b,int p)
{
if(b==0)
return 1;
if(b&1)
return (LL)a*qmi(a,b-1,p)%p; //注意这里需要强制转换一下,int会溢出
else
{
LL temp=qmi(a,b/2,p);
return temp*temp%p;
}
}
int main()
{
int n;
cin >> n;
while(n--)
{
int a,b,p;
cin >> a >> b >> p;
cout << qmi(a,b,p) << endl;
}
return 0;
}
876. 快速幂求逆元
给定 n
组 ai,pi,其中 pi 是质数,求 ai 模 pi
的乘法逆元,若逆元不存在则输出 impossible
。
注意:请返回在 0∼p−1
之间的逆元。
乘法逆元的定义
若整数 b,m
互质,并且对于任意的整数 a,如果满足 b|a,则存在一个整数 x,使得 a/b≡a×x(modm),则称 x 为 b 的模 m 乘法逆元,记为 b−1(modm)。
b 存在乘法逆元的充要条件是 b 与模数 m 互质。当模数 m 为质数时,bm−2 即为 b
的乘法逆元。
输入格式
第一行包含整数 n
。
接下来 n
行,每行包含一个数组 ai,pi,数据保证 pi
是质数。
输出格式
输出共 n
行,每组数据输出一个结果,每个结果占一行。
若 ai
模 pi
的乘法逆元存在,则输出一个整数,表示逆元,否则输出 impossible
。
数据范围
1≤n≤105
,
1≤ai,pi≤2∗109
输入样例:
3
4 3
8 5
6 3
输出样例:
1
2
impossible
* 假设 a,b,m都是整数,m>1,且ab与1模m同余,则a与b互为模m的逆元。
* 所以当m为质数的时候,a的m-2次方就是a模m的逆元。(费马小定理)。
/**
* 假设 a,b,m都是整数,m>1,且ab与1模m同余,则a与b互为模m的逆元。
* 所以当m为质数的时候,a的m-2次方就是a模m的逆元。(费马小定理)。
*/
#include <iostream>
using namespace std;
typedef long long LL;
int qmi(int a,int b,int p)
{
if(b==0)
return 1;
if(b&1)
return (LL)a*qmi(a,b-1,p)%p; //注意这里需要强制转换一下,int会溢出
else
{
LL temp=qmi(a,b/2,p);
return temp*temp%p;
}
}
int main()
{
int n;
cin >> n;
while (n -- )
{
int a,p;
cin >> a >> p;
if(a%p==0)
cout << "impossible\n";
else
cout << qmi(a,p-2,p) << endl;
}
return 0;
}
边栏推荐
- redis operation
- 3.2 - classification - Logistic regression
- 1106 2019 Sequence (15 points)
- JRS303-数据校验
- Find the latest staff salary and the last staff salary changes
- 9、Neural Sparse Voxel Fields
- 如何仅更改 QGroupBox 标题的字体?
- Conditional statements in TF; where()
- 数据库无法启动,报无法分配内存,怎么处理
- Write a resume like this, easy to get the interviewer
猜你喜欢
机器学习(二)线性回归
1091 N-自守数 (15 分)
[C语言] sscanf如何实现sscanf_s?
Redis source code-String: Redis String command, Redis String storage principle, three encoding types of Redis string, Redis String SDS source code analysis, Redis String application scenarios
FPGA 20个例程篇:11.USB2.0接收并回复CRC16位校验
【TA-霜狼_may-《百人计划》】图形3.7.2 command buffer简
3.2-分类-Logistic回归
Hibernate 的 Session 缓存相关操作
记录一些遇见的bug——Lombok和Mapstruct的冲突导致,A component required a bean of type ‘com.XXX.controller.converter.
【LeetCode】Summary of linked list problems
随机推荐
项目1-PM2.5预测
tf.cast(), reduce_min(), reduce_max()
9、Neural Sparse Voxel Fields
软件测试常用工具的用途及优缺点比较(详细)
My creative anniversary丨Thank you for being with you for these 365 days, not forgetting the original intention, and each is wonderful
Conditional statements in TF; where()
抽象类和接口
1.1-回归
C语言-结构体
[Recommender System]: Overview of Collaborative Filtering and Content-Based Filtering
4.1ROS运行管理/launch文件
Active users of mobile banking grew rapidly in June, hitting a half-year high
1071 Small Gamble (15 points)
oracle19c does not support real-time synchronization parameters, do you guys have any good solutions?
oracle数据库中列转行,列会有变化
Serverless + domain name can also build a personal blog? Really, and soon
tf中自减操作;tf.assign_sub()
场地预订系统,帮助场馆提高坪效
求职简历这样写,轻松搞定面试官
Tensorflow中使用tf.argmax返回张量沿指定维度最大值的索引