Very basic things, but not sloppy, there are 3 ways, listed below.
I. Linear inverse element
#include <iostream>#include<cstdio>#include<cmath>#include<ctime>#include<queue>#include<algorithm>#include<cstring>using namespacestd;#defineDuke (I,a,n) for (int i = a;i <= n;i++)#defineLV (i,a,n) for (int i = a;i >= n;i--)#defineClean (a) memset (A,0,sizeof (a))Const intINF =1<< -; typedefLong LongLl;typedefDoubledb;template<classT>voidRead (T &x) { CharC; BOOLOP =0; while(c = GetChar (), C <'0'|| C >'9') if(c = ='-') op =1; X= C-'0'; while(c = GetChar (), C >='0'&& C <='9') x= x *Ten+ C-'0'; if(OP) x =-x;} Template<classT>voidwrite (T x) {if(X <0) Putchar ('-'), x =-x; if(x >=Ten) Write (X/Ten); Putchar ('0'+ x%Ten);}intn;ll p,inv[3000005];voidWork () {inv[1] =1; Duke (I,2, N) {Inv[i]= (P/i) * inv[p% i]%p; } Duke (I,1, N) {printf ("%lld\n", Inv[i]); }}intMain () {read (n); Read (P); Work (); return 0;}
Two. Fermat theorem seeking inverse element
#include <iostream>#include<cstdio>#include<cmath>#include<ctime>#include<queue>#include<algorithm>#include<cstring>using namespacestd;#defineDuke (I,a,n) for (int i = a;i <= n;i++)#defineLV (i,a,n) for (int i = a;i >= n;i--)#defineClean (a) memset (A,0,sizeof (a))Const intINF =1<< -; typedefLong LongLl;typedefDoubledb;template<classT>voidRead (T &x) { CharC; BOOLOP =0; while(c = GetChar (), C <'0'|| C >'9') if(c = ='-') op =1; X= C-'0'; while(c = GetChar (), C >='0'&& C <='9') x= x *Ten+ C-'0'; if(OP) x =-x;} Template<classT>voidwrite (T x) {if(X <0) Putchar ('-'), x =-x; if(x >=Ten) Write (X/Ten); Putchar ('0'+ x%Ten);} ll N,p;ll Qpow (ll X,ll y) {ll tot=1; while(y) {if(Y &1) !=0) {tot*=x; } x*=x; X%=p; Tot%=p; Y>>=1; } returntot;}intMain () {read (n); Read (P); printf ("%lld\n", Qpow (N,p-2)); return 0;}
Three. EXGCD to seek inverse element
#include <iostream>#include<cstdio>#include<cmath>#include<ctime>#include<queue>#include<algorithm>#include<cstring>using namespacestd;#defineDuke (I,a,n) for (int i = a;i <= n;i++)#defineLV (i,a,n) for (int i = a;i >= n;i--)#defineClean (a) memset (A,0,sizeof (a))Const intINF =1<< -; typedefLong LongLl;typedefDoubledb;template<classT>voidRead (T &x) { CharC; BOOLOP =0; while(c = GetChar (), C <'0'|| C >'9') if(c = ='-') op =1; X= C-'0'; while(c = GetChar (), C >='0'&& C <='9') x= x *Ten+ C-'0'; if(OP) x =-x;} Template<classT>voidwrite (T x) {if(X <0) Putchar ('-'), x =-x; if(x >=Ten) Write (X/Ten); Putchar ('0'+ x%Ten);} ll EXGCD (ll A,ll b,ll&x,ll &y) { if(A = =1&& b = =0) {x=1; Y=0; return 1; } ll T= EXGCD (b,a%b,y,x); Y-= A/b *x; returnt;}intMain () {ll n,p,x,y; Read (n); Read (P); ll T=EXGCD (n,p,x,y); printf ("%lld\n", (x% p + p)%p); return 0;}
"Template" inverse element