当前位置:网站首页>Mathematical knowledge: fast power inverse element - fast power
Mathematical knowledge: fast power inverse element - fast power
2022-06-23 07:45:00 【Fight! Sao Nian!】
subject :AcWing 876. Fast power inverse
Given n Group ai,pi, among pi Prime number , seek ai model pi Multiplicative inverse element of , If the inverse element does not exist, output impossible.
Be careful : Please return to 0∼p−1 The inverse element between .
Definition of multiplicative inverse
If integer b,m Coprime , And for any integer a, If meet b|a, Then there is an integer x, bring a/b≡a*x(mod m), said x by b The mold m Multiplicative inverse element , Write it down as b−1(modm).
b The necessary and sufficient condition for the existence of multiplicative inverse is b And modulus m Coprime . When modulus m Prime number ,bm−2 That is to say b Multiplicative inverse element of .
Input format
The first line contains integers n.
Next n That's ok , Each row contains an array ai,pi, Data assurance pi Prime number .
Output format
The output, n That's ok , Each group of data outputs a result , Each result takes up one line .
if ai model pi The multiplicative inverse of exists , Then an integer is output , Represents the inverse element , Otherwise output impossible.
Data range
1≤n≤105,
1≤ai,pi≤2∗109
sample input :
3
4 3
8 5
6 3
sample output :
1
2
impossible
Topic analysis :
When n Prime number , You can use the fast power to find the inverse element :
a / b ≡ a * x (mod n)
Ride on both sides b Available a ≡ a * b * x (mod n)
namely 1 ≡ b * x (mod n)
Same as b * x ≡ 1 (mod n)
From Fermat's theorem , When n Prime number
b ^ (n - 1) ≡ 1 (mod n)
Remove one b Come out and get b * b ^ (n - 2) ≡ 1 (mod n)
Therefore, when n Prime number ,b Multiplicative inverse element of x = b ^ (n - 2)
#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,p;
scanf("%d%d",&a,&p);
int res=qmi(a,p-2,p);
if(a%p)cout<<res<<endl;
else cout<<"impossible"<<endl;
}
return 0;
}
边栏推荐
- 干货来了|《PaaS》合辑抢先看~
- How to quickly and gracefully download large files from Google cloud disk (II)
- mysql中多表视图性能疑惑
- Playwirght深度入门
- 《一周的朋友》
- ArcMap批量删除距离较近的点
- To conquer salt fields and vegetable fields with AI, scientific and technological innovation should also step on the "field"
- 【云计算赛项】职业技能竞赛--容器开发部分例题Pig快速开发框架
- Abnormal logic reasoning problem of Huawei software test written test
- Heterogeneous transaction scenario interaction process and consistency assurance
猜你喜欢

SimpleDateFormat 线程安全问题

论文写作之WPS安装Mathtype插件编写数学公式
![[deep learning] [original] how to detect targets and draw map and other parameter maps without yolov5 weights or models](/img/f3/ff14cb5439a24e26f977e5f0d15785.png)
[deep learning] [original] how to detect targets and draw map and other parameter maps without yolov5 weights or models

测试apk-异常管控NetTraffic攻击者开发
![[pyqt5 series] modify the counter to realize control](/img/de/c997a19ad72619b0fd2fcd0124ee1a.png)
[pyqt5 series] modify the counter to realize control

The original cloud landed in deep water, and the cloud product family of Boyun container released four values

How MySQL converts a date to a number

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

Live broadcast review | how can the container transformation of traditional applications be fast and stable?

【星球精选】如何高效构建 Roam 与 theBrain 间细粒度双向链接?
随机推荐
测试apk-异常管控NetTraffic攻击者开发
How do I install MySQL on my computer?
[深度学习][原创]如何不用yolov5权重或者模型进行目标检测和绘制map等参数图
干货来了|《PaaS》合辑抢先看~
Judge black production based on CDN and client slow log characteristics
基于51单片机的温度检测监测报警系统设计
HCIP之路第八次实验
Minio single node deployment Minio distributed deployment fool deployment process (I)
Yolov5 detecting small targets (with source code)
MySQL on duplicate key and PgSQL on conflict (primary key) handle primary key conflicts
【云计算赛项】职业技能竞赛--容器开发部分例题Pig快速开发框架
在线JSON转CSharp(C#)Class工具
MySQL系统表介绍
Design of temperature detection and alarm system based on 51 single chip microcomputer
浅谈ThreadLocal和InheritableThreadLocal,源码解析
一篇文章学会er图绘制
Difference between char and varchar
浅析 Open API 设计规范
HCIP之路
Elaborate on the operation of idea