Luogu P3811 [template] report multiplicative inverse problem solution

Topic Portal

[Title] effect

Given $ n $, $ 1 ~ n $ find multiplicative inverse in the sense film $ p $.

[Analysis] ideas

I just had a good number of single inverse yuan, and was informed of this question after the discovery that he will not do (I really still too weak), so the school a bit recursive inversion yuan.

Provided $ p = k * i + r $, may have $ k * i + r \ equiv0 (mod \ p) $, and then multiplied by $ i ^ {- 1}, r ^ {- 1} $ to obtain $ k * r ^ {- 1} + i ^ {- 1} \ equiv0 (mod \ p) $

Because $ k = \ lfloor \ frac {p} {i} \ rfloor, r = p \ mod \ i $, so $ i ^ {- 1} \ equiv - \ lfloor \ frac {p} {i} \ rfloor * (p \ mod \ i) (mod \ p) $

So we get the recursive formula it! QwQ

$$inv[i]=(-p/i*inv[p\ mod\ i]+p)mod\ p$$

【Code】

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<queue>
 7 #define g() getchar()
 8 #define rg register
 9 #define go(i,a,b) for(rg int i=a;i<=b;i++)
10 #define back(i,a,b) for(rg int i=a;i>=b;i--)
11 #define db double
12 #define ll long long
13 #define il inline
14 #define pf printf
15 #define mem(a,b) memset(a,b,sizeof(a))
16 using namespace std;
17 int fr(){
18     int w=0,q=1;
19     char ch=g();
20     while(ch<'0'||ch>'9'){
21         if(ch=='-') q=-1;
22         ch=g();
23     }
24     while(ch>='0'&&ch<='9') w=(w<<1)+(w<<3)+ch-'0',ch=g();
25     return w*q;
26 }
27 const int N=3e6+2;
28 int n,p;
29 ll inv[N];
30 int main(){
31     //freopen("","r",stdin);
32     //freopen("","w",stdout);
33     n=fr();p=fr();
34     inv[1]=1;pf("%lld\n",inv[1]);
35     go(i,2,n) inv[i]=(p-p/i)*inv[p%i]%p,pf("%lld\n",inv[i]);
36     return 0;
37 }
Code poke here

Guess you like

Origin www.cnblogs.com/THWZF/p/11569174.html