結果

問題 No.2561 みんな大好きmod 998
ユーザー InTheBloomInTheBloom
提出日時 2023-12-02 14:53:11
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 175 ms / 4,000 ms
コード長 2,450 bytes
コンパイル時間 4,686 ms
コンパイル使用メモリ 196,360 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2023-12-02 14:53:21
合計ジャッジ時間 5,526 ms
ジャッジサーバーID
(参考情報)
judge15 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,548 KB
testcase_01 AC 9 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 166 ms
6,676 KB
testcase_04 AC 175 ms
6,676 KB
testcase_05 AC 166 ms
6,676 KB
testcase_06 AC 165 ms
6,676 KB
testcase_07 AC 1 ms
6,676 KB
testcase_08 AC 1 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 1 ms
6,676 KB
testcase_11 AC 24 ms
6,676 KB
testcase_12 AC 1 ms
6,676 KB
testcase_13 AC 1 ms
6,676 KB
testcase_14 AC 1 ms
6,676 KB
testcase_15 AC 165 ms
6,676 KB
testcase_16 AC 1 ms
6,676 KB
testcase_17 AC 1 ms
6,676 KB
testcase_18 AC 1 ms
6,676 KB
testcase_19 AC 118 ms
6,676 KB
testcase_20 AC 1 ms
6,676 KB
testcase_21 AC 1 ms
6,676 KB
testcase_22 AC 1 ms
6,676 KB
testcase_23 AC 1 ms
6,676 KB
testcase_24 AC 1 ms
6,676 KB
testcase_25 AC 1 ms
6,676 KB
testcase_26 AC 32 ms
6,676 KB
testcase_27 AC 165 ms
6,676 KB
testcase_28 AC 42 ms
6,676 KB
testcase_29 AC 13 ms
6,676 KB
testcase_30 AC 41 ms
6,676 KB
testcase_31 AC 84 ms
6,676 KB
testcase_32 AC 16 ms
6,676 KB
testcase_33 AC 13 ms
6,676 KB
testcase_34 AC 167 ms
6,676 KB
testcase_35 AC 11 ms
6,676 KB
testcase_36 AC 10 ms
6,676 KB
testcase_37 AC 6 ms
6,676 KB
testcase_38 AC 24 ms
6,676 KB
testcase_39 AC 40 ms
6,676 KB
testcase_40 AC 41 ms
6,676 KB
testcase_41 AC 24 ms
6,676 KB
testcase_42 AC 42 ms
6,676 KB
testcase_43 AC 5 ms
6,676 KB
testcase_44 AC 18 ms
6,676 KB
testcase_45 AC 119 ms
6,676 KB
testcase_46 AC 13 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;

void main () {
    int N, K; readln.read(N, K);
    int[] A = readln.split.to!(int[]);

    solve(N, K, A);
}

void solve (int N, int K, int[] A) {
    /* 制約が小さいのでdpするまでもない */
    const long MOD1 = 998;
    const long MOD2 = 998244353;
    int ans = 0;
    foreach (s; enumComb(A, K)) {
        long sum1 = 0, sum2 = 0;
        foreach (x; s) {
            sum1 += x; sum1 %= MOD1;
            sum2 += x; sum2 %= MOD2;
        }
        if (sum2 <= sum1) ans++;
    }

    writeln(ans%MOD1);
}

void read (T...) (string S, ref T args) {
    auto buf = S.split;
    foreach (i, ref arg; args) {
        arg = buf[i].to!(typeof(arg));
    }
}

import std.format : format;
import std.exception : enforce;

struct CombinationIndexes {
    int n, k;
    int[] res;
    bool end = false;

    this (int n, int k) {
        this.n = n, this.k = k;
        if (n < k) end = true;
        res = new int[](k);
        foreach (i; 0..k) res[i] = i;
    }

    bool empty () const { return end; }
    auto front () const { return res; }
    void popFront () {
        bool ok = false;
        if (res[$-1] < n-1) { res[$-1]++; ok = true; return; }
        foreach_reverse (i; 0..k-1) {
            if (res[i]+1 < res[i+1]) {
                res[i]++;
                int diff = 1;
                foreach (j; i+1..k) { res[j] = res[i]+diff; diff++; }
                return;
            }
        }
        end = true;
    }
}

struct CombinationItems (T) {
    import std.traits : ForeachType, Unconst;
    alias E = Unconst!(ForeachType!T)[];
    E arr;
    E res;
    CombinationIndexes comb;
    this (T arr, int k) {
        this.arr = arr.dup;
        res = new E(k);
        comb = enumComb(cast(int) arr.length, k);
        int i = 0;
        foreach (c; comb.front) res[i++] = arr[c];
    }

    bool empty () const { return comb.empty; }
    void popFront () {
        comb.popFront;
        int i = 0;
        foreach (c; comb.front) res[i++] = arr[c];
    }
    auto front () const { return res; }
}

auto enumComb (T) (T arr, int k)
if (is (T == E[], E) || is (T == E[n], E, size_t n))
in {
    enforce(0 <= k, format("k must satisfy 0 <= n && 0 <= k. Now k = %s.", k));
}
do {
    return CombinationItems!(T)(arr, k);
}

auto enumComb (int n, int k)
in {
    enforce(0 <= n && 0 <= k, format("n, k must satisfy 0 <= n && 0 <= k. Now n = %s, k = %s.", n, k));
}
do {
    return CombinationIndexes(n, k);
}
0