同餘and乘法逆元學習筆記

sjp大佬讓我寫同餘那就只能硬着頭皮按學長的ppt來寫了,咕咕咕ios

數學符號

不想一個一個打了,湊合着看吧ui

快速冪

輸入b,p,k的值,求b^p mod k的值。spa

方法一

直接反覆平方,複雜度是\(O(n)\)基本沒戲會TLE的,不用看了3d

方法二

若是\(a\)本身乘一次就變成了\(a^2\),\(a^2\)再自乘一次就變成了\(a^4\).....乘\(n\)次就變成了\(2^n\)code

咱們將b分解成二進制看一下下htm

假設b=\(11\),分解成二進制就是\((1011)\),從左到右這些 \(1\)分別表明十進制的 \(8\),\(2\),\(1\),也就是\(a^b=a^8 \times a^2 \times a^1\)這就是快速冪的原理blog

int quick_pow(int a, int b)
{
    int ans = 1, base = a;
    while(b > 0)
    {
        if(b & 1)//和b%2!=0同樣的效果
            ans *= base;//把ans乘上對應的a^(2^n)

        base *= base;//base自乘
        b >>= 1;//位運算,b右移一位,如101變成10(把最右邊的1移掉了),10010變成1001。如今b在二進制下最後一位是剛剛的倒數第二位。
    }
    return ans;
}

同餘

概念

\(m | (a − b)\),則稱$ a \(與\) b \(對模\) m$ 同 餘,記做$ a ≡ b (mod m)$get

同餘的性質

1.自反性:\(a ≡ a\)
2.對稱性:若 \(a ≡ b\),則$ b ≡ a$
3.傳遞性:若$ a ≡ b\(,\)b ≡ c\(,則\) a ≡ c$
4.同餘式相加:若 \(a ≡ b\)\(c ≡ d\),則 \(a ± c ≡ b ± d\)
5.同餘式相乘:若 \(a ≡ b\)\(c ≡ d\),則 \(ac ≡ bd\)
6.同冪性:若\(a ≡ b(\mod m)\)\(a^n ≡ b^n(\mod m)\)
7.若\(a \mod p=x\) ,\(a \mod q= x\),則 \(p,q\)互質,則 \(a \mod p*q =x\)
證實:
略,太難打了...自行百度吧...咕咕咕數學

乘法逆元

概念:

\(ap ≡ 1 (mod m)\),則稱 \(a\)\(p\)在模 $m \(意義下互爲乘法逆 元。簡稱\) a $是 \(p\) 的逆元或$ p$ 是$$ 的逆元。爲了方便咱們常把 \(a\)
的乘法逆元記作$ a^{-1}$ 。
}
由於 \(a \times a^{-1} ≡ 1\),因此咱們能夠把$ a^{−1} \(看做\)\frac{1}{a} $。但請注意在模意義下不存在除法操做。乘法逆元可能不存在

來自谷歌的解釋:

\(a⋅a′≡1\pmod p\)
咱們稱a′是a在模p意義下的乘法逆元,記做\(a^{-1}\)
其用途和倒數相似,若要在模\(p\)意義下將\(a\)除以\(b\),不能直接\(a/b\),由於除法是不知足模運算的,此時咱們須要轉爲乘法:\(a⋅b^{-1}\)

求逆元的方法

擴展歐幾里得

假如\(b=1\),因爲\(gcd(a,b)=1\),所以\(a=x=1\)

假如\(b≠1\),不妨假設\(a=kb+r\),而且咱們已經求出了\(bx+ry=1\)的一組解\((x_0,y_0)\)

\(bx_0+(a-kb)y_0=1\)

\(ax_1+by_1=1\)

\(bx_0+ay_0-kby_0=b(x_0-ky_0)+ay_0=ax_1+by_1\)

\(x_1=y_0\)
\(y_1=x_0-ky_0\)

那麼\((x_1,y_1)\)就是\(ax+by=1\)的一組解,這不就是exgcd?

void exgcd(int a, int b, int& x, int& y) {
  if (b == 0) {
    x = 1, y = 0;
    return;
  }
  exgcd(b, a % b, y, x);
  y -= a / b * x;
}

快速冪法\(o(n*log(n))\)

p是質數

根據費馬小定理:

\(p\) 爲質數, \(a\) 爲正整數,且 \(a\)\(p\) 互質,則 \(a^{p-1} \equiv 1 \pmod p\)
\(ax \equiv 1 \pmod b\)

因此 \(ax \equiv a^{b-1} \pmod b\)

因此 \(x \equiv a^{b-2} \pmod b\)

因此咱們能夠用快速冪來算出 \(a^{p-2} \pmod p\)值,這個數就是它的逆元了

代碼就是快速冪,不會的請點這裏

遞推法\(o(n)\)

p必須是質數

\(p=ki+j,j<i,1<i<p\) ,再放到 \(\mod p\) 意義下就會獲得: \(ki+j \equiv 0 \pmod p\)

兩邊同時乘 \(i^{-1},j^{-1}\) (注意:\(1^{-1} \equiv 1 \pmod p\)

\(kj^{-1}+i^{-1} \equiv 0 \pmod p\)

\(i^{-1} \equiv -kj^{-1}+ \pmod p\)

\(i^{-1} \equiv -(\frac{p}{i}) (p \mod i)^{-1}\)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<string>
#include<cstring>
#define ll long long int
using namespace std;
const int maxn=999999999;
const int minn=-999999999;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
long long p,c[3000005];
int main()
{
    long long n;
    scanf("%lld%lld",&n,&p);
    c[1]=1;
    printf("1\n");
    for(register int i=2; i<=n; i++)
    {
        c[i]=(p-p/i)*c[p%i]%p;
        printf("%lld\n",c[i]);
    }
    return 0;
}

模板題目:

P3811 【模板】乘法逆元

代碼:

方法一:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<string>
#include<cstring>
#define ll long long int
using namespace std;
const int maxn=999999999;
const int minn=-999999999;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
long long p,c[3000005];
int main()
{
    long long n;
    scanf("%lld%lld",&n,&p);
    c[1]=1;
    printf("1\n");
    for(register int i=2; i<=n; i++)
    {
        c[i]=(p-p/i)*c[p%i]%p;
        printf("%lld\n",c[i]);
    }
    return 0;
}

方法二:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<string>
#include<cstring>
#define ll long long int
using namespace std;
const int maxn=999999999;
const int minn=-999999999;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
long long p;
long long quick_pow(long long x,long long y)
{
    long long ans=1;
    while(y!=0)
    {
        if(y&1)
        {
            ans=((ans%p)*(x%p))%p;
        }
        x=((x%p)*(x%p))%p;
        y>>=1;
    }
    return ans;
}
int main()
{
    long long n;
    scanf("%lld%lld",&n,&p);
    for( int i=1;i<=n;i++)
    {
        printf("%lld\n",(quick_pow(i,p-2))%p);
    }
    return 0;
}
相關文章
相關標籤/搜索