当前位置:网站首页>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;
}
边栏推荐
- Heterogeneous transaction scenario interaction process and consistency assurance
- What is customer experience automation?
- Test APK exception control nettraffic attacker development
- Make a record of glib2.14 upgrading glib2.18 and the principle of the steps
- 小爱音箱连接网络异常解决办法
- SimpleDateFormat 线程安全问题
- 跳跃表原理
- Sstable details
- Ntu-rgbd data set download and data format analysis
- [2022 graduation season] from graduation to transition to the workplace
猜你喜欢

干货来了|《PaaS》合辑抢先看~
three. Solution to stripe shadow and grid shadow in JS

在线JSON转CSharp(C#)Class工具

Matlab随机波动率SV、GARCH用MCMC马尔可夫链蒙特卡罗方法分析汇率时间序列

Nacos adapts to oracle11g- modify the source code of Nacos

浅谈ThreadLocal和InheritableThreadLocal,源码解析

Qt工程报错:-1: error: Cannot run compiler ‘clang++‘. Output:mingw32-make.exe

Focusing on the industry, enabling customers | release of solutions for the five industries of the cloud container cloud product family

YGG 西班牙 subDAO——Ola GG 正式成立

MIT CMS.300 Session 12 – IDENTITY CONSTRUCTION 虚拟世界中身份认同的建立 part 2
随机推荐
MySQL (IV) - MySQL storage engine
Data types in tensorflow
MySQL (V) - locks and transactions
[markdown] markdown tutorial summary
Unity to wechat applet games
Product axure9 (English version), prototype design and production pull-down secondary menu
传智教育 | 项目发布前如何打tag标签及标签命名规范
Qt工程报错:-1: error: Cannot run compiler ‘clang++‘. Output:mingw32-make.exe
基于51单片机的温度检测监测报警系统设计
[* * * array * * *]
Cirium has gradually become the standard for airlines' carbon dioxide emission reporting
Qt 使用QDomDocument读取xml文件
leetcode210. Schedule II 207 Curriculum topology sorting DFS BFS
Elaborate on the operation of idea
【PyQt5系列】修改计数器实现控制
数学知识:欧拉函数—欧拉函数
左乘右乘矩阵问题
Both are hard disk partitions. What is the difference between C disk and D disk?
Intelligence Education - how to merge codes when code conflicts occur in multi person collaborative development?
CIRIUM(睿思誉)逐渐成为航空公司二氧化碳排放报告的标准