Codeforces Round #695 (Div. 2)
D. Sum of Paths
思路
\(f[i][j]\) 表示走了 \(j\) 步到 \(i\) 的所有方案数
那么 \(f[i][j] = f[i - 1][j - 1] + f[i + 1][j - 1]\)
由于路径可逆,\(f[i][j]\) 也可以表示从 \(i\) 走了 \(j\) 步的方案数
我们要求经过 \(i\) 的所有次数
\(times[i] = f[i][j] + f[i][k - j]\)
感性理解就是先从某处走了 \(j\) 步到 \(i\),再从 \(i\) 走 \(k-j\) 步
那么此题就结束了 qwq
代码如下
点击查看代码
/********************
Author: Nanfeng1997
Contest: Codeforces Round #695 (Div. 2)
URL: https://codeforces.com/contest/{getProblemIndexes(problemCurrentPageList[i][0])[0]}/problem/D
When: 2022-03-15 20:45:20
Memory: 1024MB
Time: 3000ms
********************/
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int MOD = 1e9 + 7;
void solve() {
int n, k, q; cin >> n >> k >> q;
vector<int> w(n + 1); for(int i = 1; i <= n; i ++ ) cin >> w[i];
vector<vector<LL> > f(n + 2, vector<LL> (k + 1, 0)); for(int i = 1; i <= n; i ++ ) f[i][0] = 1;
for(int j = 1; j <= k; j ++ ) for(int i = 1; i <= n; i ++ ) {
f[i][j] = (f[i - 1][j - 1] + f[i + 1][j - 1]) % MOD;
}
vector<LL> times(n + 1);
for(int i = 1; i <= n; i ++ ) {
for(int j = 0; j <= k; j ++ ) {
times[i] = ((f[i][j] * f[i][k - j]) % MOD + times[i]) % MOD;
}
}
LL ans = 0;
for(int i = 1; i <= n; i ++ ) {
ans = (ans + times[i] * w[i]) % MOD;
}
while(q -- ) {
int id, x; cin >> id >> x;
ans = (ans - (times[id] * w[id]) % MOD + MOD) % MOD;
ans = (ans + (times[id] * x) % MOD) % MOD;
w[id] = x;
cout << ans << "\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}