洛谷 P6620 [省选联考 2020 A 卷] 组合数问题
记一下是怎么推的。
\[\sum\limits_{k = 0}^n f(k) \times x^k \times \binom{n}{k}
\]
\[= \sum\limits_{p = 0}^m \sum\limits_{k = 0}^n a_p k^p \times x^k \times \binom{n}{k}
\]
\[= \sum\limits_{p = 0}^m \sum\limits_{k = 0}^n x^k \times \binom{n}{k} \times a_p \times \sum\limits_{i = 0}^k {p \brace i} \binom{k}{i} i!
\]
\[= \sum\limits_{p = 0}^m a_p \times \sum\limits_{i = 0}^p {p \brace i} i! \times \sum\limits_{k = i}^n \binom{n}{k} \binom{k}{i} \times x^k
\]
\[= \sum\limits_{p = 0}^m a_p \times \sum\limits_{i = 0}^p {p \brace i} i! \times x^i \times \binom{n}{i} \times \sum\limits_{k = 0}^{n - i} \binom{n - i}{k} \times x^k
\]
\[= \sum\limits_{p = 0}^m a_p \times \sum\limits_{i = 0}^p {p \brace i} x^i \times n^{\underline i} \times (x + 1)^{n - i}
\]
至此可在 \(O(m^2 \log n)\) 计算。
code
// Problem: P6620 [省选联考 2020 A 卷] 组合数问题
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P6620
// Memory Limit: 512 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mems(a, x) memset((a), (x), sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<ll, ll> pii;
const int maxn = 1010;
ll n, m, X, mod, a[maxn], S[maxn][maxn], f[maxn];
inline ll qpow(ll b, ll p) {
ll res = 1;
while (p) {
if (p & 1) {
res = res * b % mod;
}
b = b * b % mod;
p >>= 1;
}
return res;
}
void solve() {
scanf("%lld%lld%lld%lld", &n, &X, &mod, &m);
for (int i = 0; i <= m; ++i) {
scanf("%lld", &a[i]);
}
S[0][0] = 1;
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= m; ++j) {
S[i][j] = (S[i - 1][j - 1] + S[i - 1][j] * j % mod) % mod;
}
}
f[0] = 1;
for (int i = 1; i <= m; ++i) {
f[i] = f[i - 1] * (n - i + 1) % mod;
}
ll ans = 0;
for (int p = 0; p <= m; ++p) {
ll res = 0;
for (int i = 0; i <= p; ++i) {
res = (res + S[p][i] * qpow(X, i) % mod * f[i] % mod * qpow((X + 1) % mod, n - i) % mod) % mod;
}
ans = (ans + res * a[p] % mod) % mod;
}
printf("%lld\n", ans);
}
int main() {
int T = 1;
// scanf("%d", &T);
while (T--) {
solve();
}
return 0;
}