1467D - Sum of Paths - CodeForces Solution


combinatorics dp math *2200

Please click on ads to support us..

C++ Code:

               //  ॐ
#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
#define ld long double
typedef pair<int,int>       pii;
typedef pair<ll,ll>       pll;
typedef vector<ll> vll;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<ll>>;
#define pb              push_back 
#define Sort(a)         sort(a.begin(),a.end())
#define ff                  first
#define ss                  second 
const int N = 1e3 + 5;
const ll f = 1e9 + 7;

void solve(){
    int n,k,q;
    cin>>n>>k>>q;

    vvll dp1(k+1,vll(n+2,0));
    for (int i = 1; i <= n; ++i)
        dp1[0][i]=1;

    for (int j = 1; j <= k; ++j){
        for (int i = 1; i <= n; ++i){
            dp1[j][i] = (dp1[j-1][i-1] + dp1[j-1][i+1])%f;
        }
    }

    vvll dp(k+1,vll(n+2,0));
    for (int j = 0; j <= k; ++j){
        for (int i = 1; i <= n; ++i){
            dp[j][i] = (dp1[j][i] * dp1[k-j][i])%f;
        }
    }

    vll sum(n+1,0); 
    for (int i = 1; i <= n; ++i){
        for (int j = 0; j <= k; ++j){
            sum[i] += dp[j][i]; sum[i]%=f;
        }
    }

    vll ar(n+1,0); ll ans = 0;
    for (int i = 1; i <= n; ++i){
        cin>>ar[i];
        ans += (sum[i]*ar[i])%f;
        ans %= f;
    }

    while(q--){
        int i, x;
        cin>>i>>x;
        ans += f - (sum[i]*ar[i])%f;
        ans %= f; ar[i] = x;
        ans += (sum[i]*ar[i])%f;
        ans %= f;
        cout<<ans<<"\n";
    }
}
 
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t=1;
    // cin>>t;
    while(t--)
        solve();
 
}


Comments

Submit
0 Comments
More Questions

1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality
1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array
1370A - Maximum GCD
149A - Business trip
34A - Reconnaissance 2
59A - Word
462B - Appleman and Card Game
1560C - Infinity Table
1605C - Dominant Character
1399A - Remove Smallest
208A - Dubstep
1581A - CQXYM Count Permutations