当前位置:网站首页>[4.4 detailed explanation of fast power and inverse element of fast power]
[4.4 detailed explanation of fast power and inverse element of fast power]
2022-07-27 00:34:00 【Romantic dog】
4.4.1 Fast power explanation
Concept
- Quickly find a k m o d p a^k\mod p akmodp Result
thought
- Preprocessing a 2 0 , a 2 1 , a 2 2 … a 2 l o g 2 k a^{2^0},a^{2^1},a^{2^2}\dots a^{2^{log_2^k}} a20,a21,a22…a2log2k Result
- Is that k = 2 p 1 + 2 p 2 + ⋯ + 2 p i k=2^{p_1}+2^{p_2}+\dots+2^{p_i} k=2p1+2p2+⋯+2pi
- namely : a k = a 2 p 1 × a 2 p 2 × ⋯ × a 2 p i a^k=a^{2^{p_1}}\times a^{2^{p_2}}\times\dots\times a^{2^{p_i}} ak=a2p1×a2p2×⋯×a2pi
- about a 2 0 × a 2 0 = a 2 1 , a 2 1 × a 2 1 = a 2 2 a^{2^0}\times a^{2^0}=a^{2^{1}},a^{2^{1}}\times a^{2^{1}}=a^{2^{2}} a20×a20=a21,a21×a21=a22, namely a 2 p i = a 2 p i − 1 × a 2 p i − 1 a^{2^{p_i}}=a^{2^{p_{i-1}}}\times a^{2^{p_{i-1}}} a2pi=a2pi−1×a2pi−1
- in summary , Record during operation a p i a^{p_i} api Value , And the result of multiplication
- take k k k Into binary representation , Bitwise
>>operation , If the current bit is 1 1 1, Then the result of the current multiplication × a p i m o d p \times a^{p_i} \mod p ×apimodp - Every time the k k k Conduct
>>After the operation , to update a p i + 1 = a p i × a p i m o d p a^{p_{i+1}}=a^{p_i}\times a^{p_i}\mod p api+1=api×apimodp - When binary k k k No more
>>when , The result of multiplication is the answer
Templates
typedef long long LL;
LL qmi(LL a,LL k,LL p){
// Calculation a^k % p Result
LL res=1; // Record the cumulative result
while(k){
if(k&1) res=res*a%p; //k&1 Get the current bit , if 1 Then I'm tired a^pi
a=a*a%p; // to update a^pi
k>>=1; // Move right 1 position
}
return res;
}
4.4.2 Fast power inverse
Concept
- congruence : set up m m m As a positive integer , a a a and b b b Is an integer , if m ∣ a − b m|a-b m∣a−b, said a a a model m m m Congruence with b b b, or a a a And b b b model m m m congruence , Write it down as a ≡ b ( m o d m ) a\equiv b(mod~m) a≡b(mod m)
- if a b ≡ 1 ( m o d m ) ab\equiv 1(mod~m) ab≡1(mod m), said b b b by a a a The mold m m m The inverse , Write it down as a − 1 ( m o d m ) a^{-1}(mod~m) a−1(mod m) or a − 1 a^{-1} a−1
Be careful
a a a The mold m m m Inverse existence ⇔ \Leftrightarrow ⇔ a a a And m m m Coprime
When m m m Prime number , Use Fermat's theorem to find
When m m m When not prime , Use the extended Euclidean algorithm to find
thought
Use fast power to realize m m m When it is a prime number, use Fermat's theorem to find the inverse element
Fermat's small Theorem : set up p p p As a prime number , And a a a And p p p Coprime , be a p − 1 ≡ 1 ( m o d p ) a^{p^{-1}}\equiv 1(mod~p) ap−1≡1(mod p)
a p − 1 ≡ 1 ( m o d p ) → a × a p − 2 ≡ 1 ( m o d p ) a × b ≡ 1 ( m o d p ) namely : b = a p − 2 \begin{aligned} a^{p^{-1}}\equiv 1(mod~p) \rightarrow &a\times a^{p^{-2}}\equiv 1(mod~p)\\ &a\times b\equiv 1(mod~p)\\ & namely :b=a^{p^{-2}} \end{aligned} ap−1≡1(mod p)→a×ap−2≡1(mod p)a×b≡1(mod p) namely :b=ap−2
Template example 876. Fast power inverse
describe
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(modm), 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
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL qmi(LL a,LL k,LL p){
LL res=1;
while(k){
if(k&1) res=res*a%p;
a=a*a%p;
k>>=1;
}
return res;
}
int main(){
int n;
cin>>n;
while(n--){
int a,p;
cin>>a>>p;
if(a%p==0) cout<<"impossible"<<endl; //a And p Non coprime means that there is no inverse element
else cout<<qmi(a,p-2,p)<<endl;
}
return 0;
}
边栏推荐
- Machine learning model -- lightgbm
- 蓝桥杯 1004 [递归]母牛的故事
- Openharmony quick start
- 【3. Vim 操作】
- 2022-07-17:1, 2, 3... N-1, N, n+1, n+2... In this sequence, only one number has repetition (n). This sequence is unordered. Find the repeated number n. This sequence is ordered. Find the repeated numb
- AutoCAD的卸载后重新安装,删除注册表的详细过程
- 信号与系统学习零输入响应
- CCPD data set processing (target detection and text recognition)
- C and pointer Chapter 18 runtime environment 18.2 interface between C and assembly language
- Request attribute in crawler
猜你喜欢

13_ Ensemble learning and random forests

Signal and system impulse response and step response

9_逻辑回归(Logistic Regression)

View where Anaconda created the environment

Arcgis和Cass实现断面展高程点

2020-12-20 九九乘法表

Complete review of parsing web pages

Leetcode topic - array

CCPD data set processing (target detection and text recognition)

Deployment of yolov5 on Jetson nano deepstream
随机推荐
Matlab based medical imaging technology filtering backprojection simulation, including direct backprojection, S-L filtering, R-L filtering, LeWitt filtering
今日份20220719折腾deeplabcut
Matlab simulation of image reconstruction using filtered back projection method
Friend友元函数以及单例模式
2020-12-22最大公因数
Ubantu installing Oracle JDK
Based on the theoretical principle and simulation results of MATLAB spherical decoding, compare 2norm spherical decoding, infinite norm spherical decoding, ML detection
V-viewer use
Downloading and processing of sentinel-2
My first blog - confused junior
Inherit, inherit, inherit
9_ Logistic regression
[PCB open source sharing] stc8a8k64d4 development board
【4.10 博弈论详解】
Web middleware log analysis script 2.0 (shell script)
我的第一篇博客-迷茫的大三人
UNET notes
画冲击函数
Go exceed API source code reading (IV) -- save (), SaveAs (name string)
AutoCAD的卸载后重新安装,删除注册表的详细过程