Inverse board

Inverse board

Pascal's Triangle

Pascal's Triangle section \ (i \) line \ (j \) column ( \ (i \) starts from 0) is the \ (C_i ^ j \)

for(int i=0;i<=n;++i){
    C[i][0]=1;
    for(int j=1;j<=i;++j)
        C[i][j]=(C[i-1][j]+C[i-1][j-1])%MOD;
}

Quick inverse power

The Fermat's little theorem \ (A \ Times A ^ {P-2} \ equiv. 1 (MOD \; P) \) (where \ (a, p \) coprime and \ (P \) is a prime number), therefore, in addition to \ (A \) is equivalent to multiplying \ (a ^ {p-2 } \) , the power is obtained using the fast \ (a ^ {p-2 } \) to

LL pow_mod(LL a, LL b, LL mo){
    LL ret = 1;
    while(b){
        if(b & 1) ret = (ret * a) % mo;
        a = (a * a) % mo;
        b >>= 1;
    }
    return ret;
}

Linear inverse

inv[1]=1;
for(int i=2;i<=n;++i){
    inv[i]=-(p/i)*inv[p%i];
    inv[i]=(inv[i]%p+p)%p;
}

Expand Euclid

It is different to Fermat's little theorem principle of rapid power to expand in Euclid \ (p \) does not necessarily have a prime number

void Exgcd(ll a, ll b, ll &x, ll &y) {
    if (!b) x = 1, y = 0;
    else Exgcd(b, a % b, y, x), y -= a / b * x;
}
int main() {
    ll x, y;
    Exgcd (a, p, x, y);
    x = (x % p + p) % p;
    printf ("%d\n", x); //x是a在mod p下的逆元
}

Guess you like

Origin www.cnblogs.com/santiego/p/11615954.html