-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path3300.cpp
73 lines (62 loc) · 1.52 KB
/
3300.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* @file 3300.cpp
* @author Macesuted ([email protected])
* @date 2023-03-20
*
* @copyright Copyright (c) 2023
*
*/
#include <bits/stdc++.h>
using namespace std;
#ifndef LOCAL
#define endl '\n'
#endif
bool mem1;
#define maxn 1005
int64_t f[maxn][maxn], g[maxn], mod;
int64_t Pow(int64_t a, int64_t x) {
int64_t ans = 1;
while (x) {
if (x & 1) ans = ans * a % mod;
a = a * a % mod, x >>= 1;
}
return ans;
}
void solve(void) {
int n, x, m;
cin >> n >> x >> mod >> m;
f[0][0] = 1;
for (int i = 0; i < m; i++)
for (int j = 0; j <= i; j++)
f[i + 1][j] = (f[i + 1][j] + f[i][j] * j) % mod, f[i + 1][j + 1] = (f[i + 1][j + 1] + f[i][j] * (j + 1)) % mod;
vector<int> vec;
for (int i = 0; i <= m; i++) {
vec.push_back(n - i + 1);
g[i] = Pow(x, i) * Pow(x + 1, n - i) % mod;
int v = i;
for (auto &j : vec) {
int c = __gcd(v, j);
v /= c, g[i] = g[i] * (j /= c) % mod;
}
}
int64_t ans = 0;
for (int i = 0, a; i <= m; i++) {
cin >> a;
for (int j = 0; j <= i; j++) ans = (ans + a * f[i][j] % mod * g[j]) % mod;
}
cout << ans << endl;
return;
}
bool mem2;
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
#ifdef LOCAL
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
#endif
int _ = 1;
while (_--) solve();
#ifdef LOCAL
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
#endif
return 0;
}