Luo Gu P3811 [template] multiplicative inverse
Topic background
This is a template problem
Title Description
Given n, p 1 ~ n find all integers in the multiplicative inverse modulo p sense.
Input Format
Line n, p
Output Format
n rows, i represents the i-th row in the inverse sense modulo p.
Sample input and output
Input # 1 copy
Output # 1 copy
Description / Tips
1 \leq n \leq 3 \times 10 ^ 6, n < p < 200005281≤n≤3×106,n<p<20000528
Input to ensure the p- the p- is a prime number.
answer:
Title to its name, title templates:
About multiplicative inverse explain please poke:
On Euler's theorem and the multiplicative inverse
Code is not open long long :( See ancestors)
#include<cstdio>
#define int long long
using namespace std;
const int maxn=3*1e6+10;
int n,p;
int inv[maxn];
signed main()
{
scanf("%lld%lld",&n,&p);
inv[1]=1;
printf("1\n");
for(int i=2;i<=n;i++)
{
inv[i]=((p-p/i)*inv[p%i])%p;
printf("%lld\n",inv[i]);
}
return 0;
}