1920E - Counting Binary Strings - CodeForces Solution


combinatorics dp math *2100

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#define bug(x) cerr<<#x<<" = "<<x<<'\n'
using namespace std;
using ll = long long;

const int mod = 998244353;
const int maxn = 2550;

int add(int a, int b) {
    return (a + b > mod ? a + b - mod : a + b);
}

int mul(int a, int b) {
    return 1ll * a * b % mod;
}

int N, K;
int dp[maxn][maxn];

void solve() {
    cin >> N >> K;
    for (int i = 1; i <= K; ++i) {
        dp[i][i - 1] = 1;
    }
    int ans = 0;
    for (int i = 1; i <= N; ++i) {
        for (int j = 0; j < K; ++j) if (dp[i][j]) {
            // if (i == 2) {
            //     cerr<<'\n';
            //     bug(j);
            //     bug(dp[i][j]);
            // }
            for (int t = 0; i + (j + 1) * t <= N; ++t) if (t <= K - 1 - j) {
                int ni = i + (j + 1) * t + (t + 1);
                if (ni <= N) 
                    dp[ni][t] = add(dp[ni][t], dp[i][j]);
                ni -= (t + 1);
                if (ni == N && t) {
                    ans = add(ans, dp[i][j]);
                    // bug(dp[i][j]);
                    // bug(t);
                    // bug(i + (j + 1) * t);
                    // bug(i);
                    // bug(j);
                    // cerr<<'\n';
                }
            }
        } 
    }
    for (int i = 0; i < K; ++i) ans = add(ans, dp[N][i]);
    cout<<ans<<'\n';
    for (int i = 0; i <= N; ++i)
        for (int j = 0; j <= K; ++j) 
             dp[i][j] = 0;
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    //freopen("gg.in", "r", stdin);
    //freopen("gg.out", "w", stdout); 
    int T; cin >> T; while (T--) solve();
}


Comments

Submit
0 Comments
More Questions

1330A - Dreamoon and Ranking Collection
1692B - All Distinct
1156C - Match Points
1675A - Food for Animals
1328C - Ternary XOR
1689A - Lex String
1708B - Difference of GCDs
863A - Quasi-palindrome
1478A - Nezzar and Colorful Balls
1581B - Diameter of Graph
404A - Valera and X
908A - New Year and Counting Cards
146A - Lucky Ticket
1594C - Make Them Equal
1676A - Lucky
1700B - Palindromic Numbers
702C - Cellular Network
1672C - Unequal Array
1706C - Qpwoeirut And The City
1697A - Parkway Walk
1505B - DMCA
478B - Random Teams
1705C - Mark and His Unfinished Essay
1401C - Mere Array
1613B - Absent Remainder
1536B - Prinzessin der Verurteilung
1699B - Almost Ternary Matrix
1545A - AquaMoon and Strange Sort
538B - Quasi Binary
424A - Squats