## Templates - multiplicative inverse number n

This question is not stored inside inva [i], but also often card. Facts have proved that reading is coming fast dancing,

``````#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int MAXN=5000005;

int a[MAXN];
ll pp[MAXN];

inline int qpow(ll x,int n,int p){
ll res=1;
while(n){
if(n&1){
res=res*x;
if(res>=p)
res%=p;
}
x=x*x;
if(x>=p)
x%=p;
n>>=1;
}
return res;
}

char c=getchar();
int x=0;
while(c<'0'||c>'9')
c=getchar();
while(c>='0'&&c<='9'){
x=x*10+c-'0';
c=getchar();
}
return x;
}

void solve(){
for(int i=1;i<=n;i++)
pp[0]=1;
for(int i=1;i<=n;i++){
pp[i]=pp[i-1]*a[i];
if(pp[i]>=p)
pp[i]%=p;
}

int invk=qpow(k,p-2,p);
ll ans=0,ki=qpow(k,n,p);
ll invpp=qpow(pp[n],p-2,p);

for(int i=n;i>=1;i--){
ll inva=invpp*pp[i-1];
if(inva>=p)
inva%=p;

ans+=ki*inva;
if(ans>=p)
ans%=p;

ki*=invk;
if(ki>=p)
ki%=p;

invpp*=a[i];
if(invpp>=p)
invpp%=p;
}

printf("%lld\n",ans);
return;
}

int main() {
#ifdef Yinku
freopen("Yinku.in","r",stdin);
#endif // Yinku
solve();
return 0;
}``````

### Guess you like

Origin www.cnblogs.com/Yinku/p/10972113.html
Recommended
Ranking
Daily