結果

問題 No.3500 01 String
コンテスト
ユーザー MERIDJA ABDELOUAHAB
提出日時 2026-04-18 00:00:56
言語 C
(gcc 15.2.0)
コンパイル:
gcc-15 -O2 -DONLINE_JUDGE -o a.out _filename_ -lm
実行:
./a.out
結果
TLE  
実行時間 -
コード長 1,619 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 255 ms
コンパイル使用メモリ 38,784 KB
実行使用メモリ 19,456 KB
最終ジャッジ日時 2026-04-18 00:01:31
合計ジャッジ時間 8,476 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 3
other TLE * 1 -- * 19
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <stdio.h>
#include <stdlib.h>

#define MOD 998244353

int main() {
    int N;
    if (scanf("%d", &N) != 1) return 0;

    char *A = (char *)malloc(N + 1);
    scanf("%s", A);

    int *pos0 = (int *)malloc(N * sizeof(int));
    int *pos1 = (int *)malloc(N * sizeof(int));
    int count0 = 0, count1 = 0;

    for (int i = 0; i < N; i++) {
        if (A[i] == '0') pos0[count0++] = i;
        else pos1[count1++] = i;
    }

    // Optimized: Only store 2 rows at a time
    // dp[2][count1 + 1]
    int *dp[2];
    dp[0] = (int *)calloc(count1 + 1, sizeof(int));
    dp[1] = (int *)calloc(count1 + 1, sizeof(int));

    dp[0][0] = 1;

    for (int i = 0; i <= count0; i++) {
        int curr = i % 2;
        int next = (i + 1) % 2;

        // Clear the 'next' row before we use it
        if (i < count0) {
            for (int j = 0; j <= count1; j++) dp[next][j] = 0;
        }

        for (int j = 0; j <= count1; j++) {
            if (dp[curr][j] == 0) continue;

            int current_idx = i + j;

            // Try placing '0' (moves to the NEXT row)
            if (i < count0 && pos0[i] <= current_idx) {
                dp[next][j] = (dp[next][j] + dp[curr][j]) % MOD;
            }

            // Try placing '1' (stays in the CURRENT row, next column)
            if (j < count1 && pos1[j] >= current_idx) {
                dp[curr][j + 1] = (dp[curr][j + 1] + dp[curr][j]) % MOD;
            }
        }
    }

    // The answer is in the row where i == count0
    printf("%d\n", dp[count0 % 2][count1]);

    free(dp[0]); free(dp[1]);
    free(pos0); free(pos1); free(A);
    return 0;
}
0