洛谷—— P3811 【模板】乘法逆元

https://www.luogu.org/problem/show?pid=3811

题目背景

这是一道模板题

题目描述

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

输入输出格式

输入格式:

一行n,p

输出格式:

n行,第i行表示i在模p意义下的逆元。

输入输出样例

输入样例#1:
10 13
输出样例#1:
1
7
9
10
8
11
2
5
3
4

说明

1 leq n leq 3 imes 10 ^ 6, n < p < 200005281n3×106​​,n<p<20000528

输入保证 pp 为质数。

线性求逆元、

 1 #include <cstdio>
 2 
 3 #define LL long long
 4 inline void read(LL &x)
 5 {
 6     x=0; register char ch=getchar();
 7     for(; ch>'9'||ch<'0'; ) ch=getchar();
 8     for(; ch>='0'&&ch<='9'; ch=getchar()) x=x*10+ch-'0';
 9 }
10 const int N(3*1e6+5);
11 LL n,p,inv[N];
12 
13 int Presist()
14 {
15     read(n),read(p);
16     inv[1]=1; puts("1");
17     for(int i=2; i<=n; ++i)
18         inv[i]=(LL)((p-p/i)*inv[p%i])%p,printf("%lld
",inv[i]);
19     return 0;
20 }
21 
22 int Aptal=Presist();
23 int main(){;}
——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
原文地址:https://www.cnblogs.com/Shy-key/p/7577310.html