Codeforces
CF Step
Youtube Linkedin Discord Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Code : No Consecutive Zeroes

#include <bits/stdc++.h>
using namespace std;

const int mod = 998244353;

void inc(int &a, int b) {
    a += b;
    a %= mod;
}

void solve(int n) {
    vector<vector<int>> dp(n + 1, vector<int>(2, 0));
    // dp[i][ch] is the number of good binary strings of length n that ends at
    // the character ch.

    dp[0][1] = 1;
    for (int i = 0; i < n; i++) {
        // Append a one.
        inc(dp[i + 1][1], dp[i][0]);
        inc(dp[i + 1][1], dp[i][1]);

        // Append a zero.
        inc(dp[i + 1][0], dp[i][1]);
    }

    int ans = 0;
    inc(ans, dp[n][0]);
    inc(ans, dp[n][1]);
    cout << ans << "\n";
}

int main() {
    int t;
    cin >> t;
    for (int i = 0; i < t; i++) {
        int n;
        cin >> n;
        solve(n);
    }
    return 0;
}