求逆元

洛谷P3811 

https://www.cnblogs.com/zjp-shadow/p/7773566.html

(一)快速幂(费马小定理和欧拉定理均可证明)

https://www.cnblogs.com/-citywall123/p/10673191.html

(二)扩展欧几里得

 

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

ll inv(ll a, ll b)
{
    ll x, y;
    exgcd(a, b, x, y);
    return (x % b + b) % b;
}

int main()
{
    int n, p;
    scanf("%d %d", &n, &p);
    for(int i = 1; i <= n; ++i)
        printf("%lld\n", inv(i, p));
}

 

 

 

(三)线性递推公式

posted @ 2020-12-11 11:19  .Ivorelectra  阅读(57)  评论(0编辑  收藏  举报