当前位置:网站首页>[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;
}
边栏推荐
- 用New,delete和用malloc,free申请,释放堆区空间
- 机器人学台大林教授课程笔记
- [PCB open source sharing] stc32g12k128/stc8h8k64u development board
- Find method of web page parsing by crawler
- C and pointer Chapter 18 runtime efficiency 18.3 runtime efficiency
- Error generating yolov5.wts file
- Shufflenet series (2): explanation of shufflenet V2 theory
- Leetcode - linked list
- View where Anaconda created the environment
- 裁剪tif影像
猜你喜欢

运算符重载

SSRF (server side request forgery) -- Principle & bypass & Defense

信号与系统冲激响应与阶跃响应

Deployment of yolov5 on Jetson nano deepstream

Recbole use 1

AutoCAD的卸载后重新安装,删除注册表的详细过程

C language to find prime numbers, leap years and minimum common multiples and maximum common divisors

CDs simulation of minimum dominating set based on MATLAB

Find method of web page parsing by crawler

Drawing warehouse Tsai
随机推荐
【3. 基础搜索与图论初识】
Find method of web page parsing by crawler
2020-12-22 maximum common factor
【4.6 中国剩余定理详解】
【Codeforces Round #808 (Div 2.) A·B·C】
Knowledge distillation -- pytorch implementation
Three tier architecture simulation
机器人学台大林教授课程笔记
Configure deeplobcut 1 with your head covered
[Qt]属性
用New,delete和用malloc,free申请,释放堆区空间
2020-12-20 九九乘法表
Comparative simulation of LEACH protocol performance, including the number of dead nodes, data transmission, network energy consumption, the number of cluster heads and load balance
Deploy yolov5 error reporting in pycharm
Leetcode - hash table
Go exceed API source code reading (IV) -- save (), SaveAs (name string)
【AtCoder Beginner Contest 261 (A·B·C·D)】
6_梯度下降法(Gradient Descent)
裁剪tif影像
C language to find prime numbers, leap years and minimum common multiples and maximum common divisors