P5431 [template] multiplicative inverse 2

P5431 [template] multiplicative inverse 2

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxn = 5e6+5;
 5 int a[maxn], pre[maxn], suf[maxn];
 6 char buf[700000000];
 7 int cnt = 0;
 8 template<typename T> inline T read(T a) {
 9     T res = 0, f = 1;
10     char ch = buf[cnt++];
11     //char ch = getchar();
12     while (!isdigit(ch)) {
13         if (ch == '-') f = -1;
14         ch = buf[cnt++];
15         //ch = getchar();
16     }
17     while (isdigit(ch)) {
18         res = (res<<3)+(res<<1) + ch - 48;
19         ch = buf[cnt++];
20         //ch = getchar();
21     }
22     return res*f;
23 }
24 
25 void exgcd(ll a, ll b, ll &x, ll &y) {
26     if (b == 0) x = 1, y = 0;
27     else exgcd(b, a%b, y, x), y -= a/b * x;
28 }
29 int main() {
30     fread(buf,1,700000000,stdin);
31     int n, p, k; n = read(n), p = read(p), k = read(k);
32     pre[0] = suf[n+1] = 1;
33     for (int i = 1; i <= n; ++i) {
34         a[i] = read(a[i]);
35         pre[i] = (ll)pre[i-1]*a[i]%p;
36     }
37     for (int i = n; i >= 1; --i) {
38         suf[i] = (ll)suf[i+1]*a[i]%p;
39     }
40     ll ans = 0;
41     for (int i = 1, kk = k; i <= n; ++i, kk = (ll)kk*k%p) {
42         ans = (ans + (ll)kk * pre[i-1]%p * suf[i+1]%p)%p;
43     }
44     ll x, y;
45     exgcd(pre[n],p,x,y);
46     if (x < 0) x += p;
47     printf("%lld\n", ans*x%p);
48     return 0;
49 }

 

Guess you like

Origin www.cnblogs.com/wstong/p/11776343.html