結果

問題 No.1693 Invasion
ユーザー yu-ta38yu-ta38
提出日時 2021-10-01 22:40:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 28 ms / 2,000 ms
コード長 2,205 bytes
コンパイル時間 700 ms
コンパイル使用メモリ 79,792 KB
実行使用メモリ 15,656 KB
最終ジャッジ日時 2023-09-26 20:59:09
合計ジャッジ時間 2,456 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
15,328 KB
testcase_01 AC 13 ms
15,372 KB
testcase_02 AC 13 ms
15,328 KB
testcase_03 AC 13 ms
15,476 KB
testcase_04 AC 13 ms
15,316 KB
testcase_05 AC 13 ms
15,388 KB
testcase_06 AC 13 ms
15,312 KB
testcase_07 AC 14 ms
15,376 KB
testcase_08 AC 14 ms
15,388 KB
testcase_09 AC 22 ms
15,400 KB
testcase_10 AC 17 ms
15,460 KB
testcase_11 AC 13 ms
15,596 KB
testcase_12 AC 18 ms
15,480 KB
testcase_13 AC 17 ms
15,656 KB
testcase_14 AC 17 ms
15,400 KB
testcase_15 AC 15 ms
15,460 KB
testcase_16 AC 17 ms
15,392 KB
testcase_17 AC 13 ms
15,300 KB
testcase_18 AC 27 ms
15,452 KB
testcase_19 AC 13 ms
15,536 KB
testcase_20 AC 13 ms
15,324 KB
testcase_21 AC 27 ms
15,596 KB
testcase_22 AC 24 ms
15,444 KB
testcase_23 AC 28 ms
15,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <set>
using namespace std;

const int MAX = 510000;
const int MOD = 998244353;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

// a^{-1} mod を計算する
long long modinv(long long a, long long mod) {
    return modpow(a, mod - 2, mod);
}

int main() {

    COMinit();
    int n, m; cin >> n >> m;
    long long mod = 998244353;
    vector<int> a(n);
    vector<int> frag(m+1,0);
    for(int i = 0; i < n; i++) {
        cin >> a[i];
        frag[a[i]] = 1;
    }
    for(int i = 0; i < n; i++) {
        for(int j = 1; j <= m; j++) {
            if(frag[j] == 0) continue;
            if(j + a[i] > m) break;

            if(frag[j+a[i]] == 0) frag[j+a[i]] = frag[j] + 1;
            if(frag[j+a[i]] > frag[j] + 1) frag[j+a[i]] = frag[j] + 1;
        }
    }
    long long ans = 0;
    for(int j = 1; j <= m; j++) {
        // cout << j << ':';
        if(frag[j] == 0) {
            // cout << 0 << endl;
            continue;
        }

        long long value = 1, x = m - frag[j], r = j - frag[j], i = 1;
        if(r == 0) ans = (ans + 1) % mod;
        else {
            // while(i <= r) {
            //     value = value * (x - i + 1);
            //     value %= mod;
            //     value =  value * modinv(i, mod) % mod;
            //     i++;
            // }
            value = COM(x,r);
            ans = (ans + value) % mod;
        }
        // cout << value << endl;
    }
    cout << ans + 1 << endl;
    return 0;
}
0