結果

問題 No.2541 Divide 01 String
ユーザー InTheBloomInTheBloom
提出日時 2023-11-24 22:58:35
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 1,813 bytes
コンパイル時間 2,775 ms
コンパイル使用メモリ 161,520 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2023-11-24 22:58:39
合計ジャッジ時間 3,609 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 1 ms
6,676 KB
testcase_04 AC 3 ms
6,676 KB
testcase_05 AC 4 ms
6,676 KB
testcase_06 AC 3 ms
6,676 KB
testcase_07 AC 4 ms
6,676 KB
testcase_08 AC 5 ms
6,676 KB
testcase_09 AC 4 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 2 ms
6,676 KB
testcase_13 AC 1 ms
6,676 KB
testcase_14 AC 1 ms
6,676 KB
testcase_15 AC 1 ms
6,676 KB
testcase_16 AC 1 ms
6,676 KB
testcase_17 AC 2 ms
6,676 KB
testcase_18 AC 1 ms
6,676 KB
testcase_19 AC 2 ms
6,676 KB
testcase_20 AC 2 ms
6,676 KB
testcase_21 AC 2 ms
6,676 KB
testcase_22 AC 1 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;

void main () {
    int N = readln.chomp.to!int;
    string S = readln.chomp;

    solve(N, S);
}

void solve (int N, string S) {
    /*
       dp的に解く
       新しい区間を導入したとき、そこで切るか切らないかの2択
     */
    bool ok = false;
    foreach (c; S) if (c == '1') ok = true;
    if (!ok) {
        writeln(0);
        return;
    }

    const int MOD = 998244353;

    long ans = 1; // カットしないやつ

    // 右と左の0を削除
    foreach (i, c; S) if (c == '1') { S = S[i..$]; break; }
    foreach_reverse (i, c; S) if (c == '1') { S = S[0..i+1]; break; }

    int len = 0;
    int cur = 1;
    while (true) {
        if (cur == S.length) {
            break;
        }
        if (S[cur] == '1') {
            ans *= len+2;
            ans %= MOD;
            len = -1;
        }
        len++;
        cur++;
    }

    writeln(ans);
}

long modPow (long a, long x, ref const long MOD) {
    import std.exception : enforce;

    enforce(0 <= x, "x must satisfy 0 <= x");
    enforce(1 <= MOD, "MOD must satisfy 1 <= MOD");
    enforce(MOD <= int.max, "MOD must satisfy MOD*MOD <= long.max");

    // normalize
    a %= MOD; a += MOD; a %= MOD;

    long res = 1L;
    long base = a;
    while (0 < x) {
        if (0 < (x&1)) (res *= base) %= MOD;
        (base *= base) %= MOD;
        x >>= 1;
    }

    return res % MOD;
}

T modPow (T, X, Y) (T a, X x, ref const Y MOD) {
    import std.exception: enforce;

    enforce(0 <= x, "x must satisfy 0 <= x");
    enforce(1 <= MOD, "MOD must satisfy 1 <= MOD");

    // normalize
    a %= MOD; a += MOD; a %= MOD;

    T res = 1;
    T base = a;
    while (0 < x) {
        if (0 < (x&1)) (res *= base) %= MOD;
        (base *= base) %= MOD;
        x >>= 1;
    }

    return res % MOD;
}
0