Solution to a problem P3811 [template] multiplicative inverse

Is intended to seek the title \ (I \) in the mold \ (P \) in the sense of the inverse element \ (\ frac {1} { i} \) i.e. \ (INV (I) \) . Obviously the scope of the subject data specified in claim inversing a linear algorithm.

Order \ (P = AI + B \) , there are:
\ [AI + B \ equiv 0 (\ MOD P) \]
transposing obtained:
\ [AI \ equiv -b (\ MOD P) \]
coefficient simplification obtained:
\ [I \ equiv - \ FRAC {B} {A} (\ MOD P) \]
taking the inverse obtained:
\ [\ FRAC {. 1} {I} \ equiv - \ FRAC {A} {B} (\ mod p) \]
i.e.
\ [inv (i) \ equiv
- \ frac {a} {b} (\ mod p) \] to avoid negative deformation obtained:
\ [INV (I) \ equiv \ FRAC {PA} {b} (\ mod p)
\] wherein:
\ [A = P / I, P% B = I \] .

\(code:\)

#include<bits/stdc++.h>//P3811 【模板】乘法逆元
using namespace std;
#define re register
#define ll long long
#define il inline
#define dou double
#define un unsigned
il int read()
{
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}
#define INF 114514114
#define clr(x) memset(x,0,sizeof(x))
#define N 3000000+10
int n,p;
int inv[N];
int main()
{
    n=read();p=read();
    inv[1]=1;
    for(re ll i=2;i<=n;i++)inv[i]=(ll)(p-p/i)*inv[p%i]%p;
    for(re ll i=1;i<=n;i++)printf("%d\n",inv[i]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/Hakurei-Reimu/p/11485640.html