当前位置:网站首页>[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;
}
边栏推荐
- Mysql互不关联的联表查询(减少了查询的次数)
- [LeetCode] 无重复最长字符串
- 【2. Tmux 操作】
- 今日份20220719折腾deeplabcut
- C language to find prime numbers, leap years and minimum common multiples and maximum common divisors
- 【AtCoder Beginner Contest 261 (A·B·C·D)】
- Deep learning of parameter adjustment skills
- [Qt]解决中文乱码问题
- Helicopter control system based on Simulink
- 寻找真凶
猜你喜欢

Convolutional neural network -- lenet (pytorch Implementation)

Leetcode - linked list

postman的使用

C and pointer Chapter 18 runtime environment 18.1 judgment of runtime environment

今日份20220719折腾deeplabcut

Drawing warehouse Tsai

Downloading and processing of sentinel-2

TypeScript(tsconfig.json)

3_Jupyter Notebook, numpy和matplotlib

01 knapsack problem 416. Segmentation and equal sum subset -494. Goal and
随机推荐
Complete review of parsing web pages
【2. Tmux 操作】
RecBole使用1
我的第一篇博客-迷茫的大三人
Helicopter control system based on Simulink
Web middleware log analysis script 1.0 (shell script)
View where Anaconda created the environment
2020-12-20 九九乘法表
Lt9611ux Mipi to HDMI 2.0 dual port with audio
信号与系统冲激响应与阶跃响应
01 knapsack problem 416. Segmentation and equal sum subset -494. Goal and
C language shutdown applet
【4.4 快速幂详解及快速幂求逆元】
Drawing warehouse-3 (functional image)
Deep learning of parameter adjustment skills
【3. 基础搜索与图论初识】
Configure deeplobcut2 with your head covered
CDs simulation of minimum dominating set based on MATLAB
蒙着头配置deeplabcut2
Visual studio C cs0006 C failed to find metadata file