#include <bits/stdc++.h>
using namespace std;
#define fastio cin.tie(0) -> sync_with_stdio(0)
//jiangly Codeforces
int P = 1e9+7;
using i64 = long long;
// assume -P <= x < 2P
int norm(int x) {
if (x < 0) {
x += P;
}
if (x >= P) {
x -= P;
}
return x;
}
template<class T>
T power(T a, i64 b) {
T res = 1;
for (; b; b /= 2, a *= a) {
if (b % 2) {
res *= a;
}
}
return res;
}
struct Z {
int x;
Z(int x = 0) : x(norm(x)) {}
Z(i64 x) : x(norm((int)(x % P))) {}
int val() const {
return x;
}
Z operator-() const {
return Z(norm(P - x));
}
Z inv() const {
assert(x != 0);
return power(*this, P - 2);
}
Z &operator*=(const Z &rhs) {
x = i64(x) * rhs.x % P;
return *this;
}
Z &operator+=(const Z &rhs) {
x = norm(x + rhs.x);
return *this;
}
Z &operator-=(const Z &rhs) {
x = norm(x - rhs.x);
return *this;
}
Z &operator/=(const Z &rhs) {
return *this *= rhs.inv();
}
friend Z operator*(const Z &lhs, const Z &rhs) {
Z res = lhs;
res *= rhs;
return res;
}
friend Z operator+(const Z &lhs, const Z &rhs) {
Z res = lhs;
res += rhs;
return res;
}
friend Z operator-(const Z &lhs, const Z &rhs) {
Z res = lhs;
res -= rhs;
return res;
}
friend Z operator/(const Z &lhs, const Z &rhs) {
Z res = lhs;
res /= rhs;
return res;
}
friend std::istream &operator>>(std::istream &is, Z &a) {
i64 v;
is >> v;
a = Z(v);
return is;
}
friend std::ostream &operator<<(std::ostream &os, const Z &a) {
return os << a.val();
}
};
#define DEBUG 0
void debug(const char* p){
#if DEBUG
freopen(p, "r", stdin);
#else
fastio;
#endif
}
int main(void){
debug("test1.txt");
int n, m, v;
cin >> n >> m >> v;
Z zv = Z(v);
vector<Z> a(n+1);
for(int i = 1; i <= n; ++i) cin >> a[i];
vector<vector<Z>> dp(n+1, vector<Z>(min(n, m)+1));
dp[0][0] = 1;
Z invn = Z(n).inv(), ans = 0;
for(int r = 0; r < n; ++r){
for(int s = 0; s <= r && s <= m; ++s){
dp[r+1][s] += (a[r+1] + zv * s) * dp[r][s];
if(s < m) dp[r+1][s+1] += Z(r+1) * invn * zv * Z(m - s) * dp[r][s];
}
}
for(int s = 0; s <= min(n, m); ++s) ans += dp[n][s];
cout << ans << "\n";
}
Thank you for your notes!
There seem to be small typos though which confused me for a second. For example: it says that "our solution is $$$E\left[\prod_{i = 1}^n(a_i + x_{i, j})\right]$$$" but I imagine you mean $$$E\left[\prod_{i = 1}^n(a_i + \sum_{i = 1} ^ m {x_{i, j}})\right]$$$, because the indicator RV $$$x_{i, j}$$$ you state contributes $$$v$$$ with probability $$$i / n$$$ and contributes $$$0$$$ otherwise and of course this contribution of $$$v$$$ can occur in any of the $$$m$$$ operations not a single one. But so much as I understand, if we basically expand all of the terms out, each term will be of type 1: $$$a_i * x_{u, v}$$$ or type 2: $$$x_{u_1, v_1} * x_{u_2, v_2}$$$. For type 1 EV can be calculated due to independence. And for type 2, we can calculate this EV a little differently. And the use of DP in this problem is so we can calculate these terms more quickly than one by one. Hope my overall understanding of solution is correct.
Thank you for your clarification! You are right, sorry for my typos! That was a typo.
Auto comment: topic has been updated by dfsof (previous revision, new revision, compare).
I did a problem (ABC231G) before with similar tricks about random operation, so I recommend you to solve it.
Thank you!