[Luogu P3811] 【模板】乘法逆元

给定n,p求1~n中所有整数在模p意义下的乘法逆元。

$O(n)$递推

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 3000000 + 10;
ll n, p, inv[maxn];
int main(){
    cin >> n >> p;
    inv[0] = 0;
    inv[1] = 1;
    for(int i = 2; i <= n; i++)
        inv[i] = i * (p / i) % p * (p / i) % p * inv[p % i] % p * inv[p % i] % p;
    for(int i = 1; i <= n; i++)
        printf("%lld\n", inv[i]);
    return 0;
} 

猜你喜欢

转载自www.cnblogs.com/ruoruoruo/p/11772197.html