1716D - Chip Move - CodeForces Solution


brute force dp math

Please click on ads to support us..

Python Code:

def solve():
    MOD = 998244353
    n, k = map(int, input().split())
    m, x = 0, 0
    while x <= n:
        x += k+m
        m += 1
    ans = [0]*(n+1)
    dp = [0]*(n+1)
    dp[0] = 1
    x = 0
    for j in range(k, k+m):
        dp1 = [0]*(n+1)
        x += j
        for i in range(x, n+1):
            dp1[i] = (dp1[i]+dp[i-j]+dp1[i-j])%MOD
        dp = dp1
        for i in range(x, n+1):
            ans[i] = (ans[i]+dp[i])%MOD
        
    print(*ans[1:])


import sys
input = lambda: sys.stdin.readline().rstrip()
solve()

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second 
#define N 200100
const int mod = 998244353;
template<typename T>void print(vector<T>&a,int c=0) {
    for(auto&it :a)cout << it+c<< " ";
    cout << endl;
}
void fast() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
template<typename T>T lcm(T a, T b) {
    return (a * b) / (__gcd(a, b));
}

int main(){
    fast();
    
    /* Code starts here -------------------------------------------> */
    int t = 1;
    // cin>>t;
    while(t--) {
        int n;cin>>n;
        int k;cin>>k;
        vector<int>ans(n + 1, 0), dp1(n + 1, 0);
        dp1[0] = 1;
        for(int mn=0;mn<=n;mn+=(k++)) {
            vector<int>dp2(n + 1, 0);
            for(int i=mn+k;i<=n;i++) {
                dp2[i] = (dp2[i - k] + dp1[i - k]) % mod;
                ans[i] = (ans[i] + dp2[i]) % mod;
            }
            swap(dp1,dp2);
        }
        for(int i=1;i<=n;i++)
            cout << ans[i] << ' ';
        cout << '\n';
    }
    
    
    
    

    
    
    
    
    
    
    

    
    
    
    
    
    return 0;
}


Comments

Submit
0 Comments
More Questions

221. Maximal Square
1200. Minimum Absolute Difference
1619B - Squares and Cubes
1619A - Square String
1629B - GCD Arrays
1629A - Download More RAM
1629C - Meximum Array
1629D - Peculiar Movie Preferences
1629E - Grid Xor
1629F1 - Game on Sum (Easy Version)
2148. Count Elements With Strictly Smaller and Greater Elements
2149. Rearrange Array Elements by Sign
2150. Find All Lonely Numbers in the Array
2151. Maximum Good People Based on Statements
2144. Minimum Cost of Buying Candies With Discount
Non empty subsets
1630A - And Matching
1630B - Range and Partition
1630C - Paint the Middle
1630D - Flipping Range
1328A - Divisibility Problem
339A - Helpful Maths
4A - Watermelon
476A - Dreamoon and Stairs
1409A - Yet Another Two Integers Problem
977A - Wrong Subtraction
263A - Beautiful Matrix
180C - Letter
151A - Soft Drinking
1352A - Sum of Round Numbers