You are viewing a single comment's thread. Return to all comments →
O(300^2 + n * m^2)
long mod = 1000000007; void combinatorial(vector<vector<long>>& binomial) { binomial[1][0] = 1; binomial[1][1] = 1; for (int n = 2; n <= 300; n++) { binomial[n][0] = 1; for (int k = 1; k <= n; k++) binomial[n][k] = (binomial[n-1][k-1] + binomial[n-1][k]) % mod; } } int gridWalking(int n, int m, const vector<int>& x, const vector<int>& D, const vector<vector<long>>& binomial) { vector<vector<long>> oneDimension(n+1, vector<long>(m+1)), multiDimension(n+1, vector<long>(m+1)), endPoint = {{0}}; for (int i = 1; i <= n; i++) { oneDimension[i][0] = 1; endPoint.emplace_back(vector<long>(D[i-1] + 2)); endPoint.back()[x[i-1]] = 1; for (int K = 1; K <= m; K++) { long total = 0, temp1 = 0, temp2; for (int point = 1; point <= D[i-1]; point++) { temp2 = endPoint[i][point]; endPoint[i][point] = (temp1 + endPoint[i][point + 1]) % mod; total = (total + endPoint[i][point]) % mod; temp1 = temp2; } oneDimension[i][K] = total; } } multiDimension[1] = oneDimension[1]; for (int i = 2; i <= n; i++) { multiDimension[i][0] = 1; for (int K = 1; K <= m; K++) { long temp = 0; for (int l = 0; l <= K; l++) temp = (temp + multiDimension[i-1][l] * (oneDimension[i][K-l] * binomial[K][l] % mod)) % mod; multiDimension[i][K] = temp; } } return multiDimension[n][m]; } int main() { int t, n, m, k; vector<vector<long>> binomial(301, vector<long>(301)); combinatorial(binomial); cin >> t; for (int i = 1; i <= t; i++) { vector<int> x, D; cin >> n >> m; for (int j = 1; j <= n; j++) { cin >> k; x.push_back(k); } for (int j = 1; j <= n; j++) { cin >> k; D.push_back(k); } cout << gridWalking(n, m, x, D, binomial) << '\n'; } }
Seems like cookies are disabled on this browser, please enable them to open this website
Grid Walking
You are viewing a single comment's thread. Return to all comments →
O(300^2 + n * m^2)