当前位置:网站首页>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;
}
边栏推荐
- Unity picture loading and saving
- 一篇文章学会er图绘制
- 左乘右乘矩阵问题
- 数学知识:快速幂—快速幂
- Which company would like to buy serious illness insurance in 2022?
- [deep learning] [original] how to detect targets and draw map and other parameter maps without yolov5 weights or models
- Minio single node deployment Minio distributed deployment fool deployment process (I)
- YGG 西班牙 subDAO——Ola GG 正式成立
- NFS 特别注意权限的问题
- [veusz] import 2D data in CSV
猜你喜欢

Ntu-rgbd data set download and data format analysis

C WPF additional attribute implementation interface defines decorator

某年某月某公司的面试题(1)

How MySQL converts a date to a number

Eureka服务注册与发现

在kubernetes中部署kubersphere

启动appium

HCIP之路第八次实验

在线文本过滤小于指定长度工具

Can you think of a better way to solve the problem of string inversion?
随机推荐
279. perfect square
Heterogeneous transaction scenario interaction process and consistency assurance
聊聊服务治理中的路由设计
Unity图片加载和保存
What is distributed?
干货来了|《PaaS》合辑抢先看~
3DMAX plug-in development environment configuration and fileexport and utilities template testing
某年某月某公司的面试题(1)
数学知识:快速幂—快速幂
浅析 Open API 设计规范
U-Net: Convolutional Networks for Biomedical Image Segmentation
C WPF realizes dynamic loading of controls through binding
HCIP之路
Friends of the week
Online text filter less than specified length tool
Eureka service registration and discovery
数学知识:欧拉函数—欧拉函数
Online JSON to CSharp (c) class tool
Wechat multiplayer chat and Roulette Games (websocket Implementation)
three. Solution to stripe shadow and grid shadow in JS