diff --git a/P3811.cpp b/P3811.cpp new file mode 100644 index 0000000..18c2b25 --- /dev/null +++ b/P3811.cpp @@ -0,0 +1,36 @@ +#include +using namespace std; +long long n, p; +long long mul[3000005]; +long long revs[3000005]; +long long rev[3000005]; +long long qpow(long long a, long long b) { + a %= p; + long long res = 1; + while(b) { + if(b & 1) { + res = (res * a) % p; + } + a = (a * a) % p; + b >>= 1; + } + return res; +} +int main() { + scanf("%lld%lld", &n, &p); + mul[0] = 1; + for(int i = 1; i <= n; ++i) { + mul[i] = (mul[i - 1] * i) % p; + } + revs[n] = qpow(mul[n], p - 2); + for(int i = n - 1; i >= 1; --i) { + revs[i] = ((i + 1) * revs[i + 1]) % p; + } + for(int i = 1; i <= n; ++i) { + rev[i] = (mul[i - 1] * revs[i]) % p; + } + for(int i = 1; i <= n; ++i) { + printf("%lld\n", rev[i]); + } + return 0; +} \ No newline at end of file diff --git a/P3811.md b/P3811.md new file mode 100644 index 0000000..f655ed6 --- /dev/null +++ b/P3811.md @@ -0,0 +1,48 @@ +# P3811 【模板】模意义下的乘法逆元 + +## 题目背景 + +这是一道模板题。 + +## 题目描述 + +给定正整数 $n,p$,求 $[1,n]$ 中所有整数在模 $p$ 意义下的乘法逆元。 + +$a$ 模 $p$ 的乘法逆元定义为 $ax\equiv1\pmod p$ 的解。 + +## 输入格式 + +一行两个正整数 $n,p$。 + +## 输出格式 + +输出 $n$ 行,其中第 $i$ 行表示 $i$ 在模 $p$ 下的乘法逆元。 + +## 输入输出样例 #1 + +### 输入 #1 + +``` +10 13 +``` + +### 输出 #1 + +``` +1 +7 +9 +10 +8 +11 +2 +5 +3 +4 +``` + +## 说明/提示 + +所有数据满足 $ 1 \leq n \leq 3 \times 10 ^ 6$,$n < p < 20000528 $。 + +输入保证 $ p $ 为质数。 \ No newline at end of file