結果

問題 No.1685 One by One
ユーザー 👑 hitonanodehitonanode
提出日時 2021-08-07 15:17:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 254 ms / 3,000 ms
コード長 3,074 bytes
コンパイル時間 912 ms
コンパイル使用メモリ 86,304 KB
実行使用メモリ 65,984 KB
最終ジャッジ日時 2023-09-12 06:17:37
合計ジャッジ時間 6,290 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 30 ms
12,556 KB
testcase_04 AC 9 ms
5,800 KB
testcase_05 AC 147 ms
33,520 KB
testcase_06 AC 53 ms
21,252 KB
testcase_07 AC 59 ms
21,840 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 8 ms
5,456 KB
testcase_10 AC 182 ms
54,524 KB
testcase_11 AC 5 ms
4,564 KB
testcase_12 AC 4 ms
4,376 KB
testcase_13 AC 63 ms
24,496 KB
testcase_14 AC 23 ms
7,768 KB
testcase_15 AC 204 ms
46,072 KB
testcase_16 AC 7 ms
5,116 KB
testcase_17 AC 4 ms
4,744 KB
testcase_18 AC 8 ms
5,196 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 57 ms
4,460 KB
testcase_21 AC 13 ms
4,380 KB
testcase_22 AC 65 ms
6,416 KB
testcase_23 AC 19 ms
6,548 KB
testcase_24 AC 181 ms
55,796 KB
testcase_25 AC 225 ms
60,908 KB
testcase_26 AC 193 ms
60,136 KB
testcase_27 AC 228 ms
57,212 KB
testcase_28 AC 213 ms
65,984 KB
testcase_29 AC 253 ms
65,832 KB
testcase_30 AC 213 ms
65,880 KB
testcase_31 AC 254 ms
65,984 KB
testcase_32 AC 253 ms
65,828 KB
testcase_33 AC 248 ms
65,680 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 67 ms
4,380 KB
testcase_39 AC 66 ms
4,376 KB
testcase_40 AC 15 ms
4,380 KB
testcase_41 AC 15 ms
4,376 KB
testcase_42 AC 1 ms
4,376 KB
testcase_43 AC 2 ms
4,380 KB
testcase_44 AC 2 ms
4,380 KB
testcase_45 AC 2 ms
4,380 KB
testcase_46 AC 59 ms
23,112 KB
testcase_47 AC 65 ms
23,164 KB
testcase_48 AC 60 ms
23,384 KB
testcase_49 AC 65 ms
23,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <iostream>
#include <vector>
#include <atcoder/modint>
using namespace std;

using mint = atcoder::modint1000000007;
// atcoder::static_modint<P>, P: prime number
template <typename modint> struct acl_fac {
    std::vector<modint> facs, facinvs;
    acl_fac(int N) {
        assert(-1 <= N and N < modint::mod());
        facs.resize(N + 1, 1);
        for (int i = 1; i <= N; i++) facs[i] = facs[i - 1] * i;
        facinvs.assign(N + 1, facs.back().inv());
        for (int i = N; i > 0; i--) facinvs[i - 1] = facinvs[i] * i;
    }
    modint ncr(int n, int r) const {
        if (n < 0 or r < 0 or n < r) return 0;
        return facs[n] * facinvs[r] * facinvs[n - r];
    }
    modint operator[](int i) const { return facs[i]; }
    modint finv(int i) const { return facinvs[i]; }
};

int main() {
    int N, M;
    cin >> N >> M;
    acl_fac<mint> fac(N + M);
    vector precalc(N + 1, vector<mint>(M + 1));
    precalc[0][0] = 1;
    for (int n = 0; n <= N; n++) {
        for (int m = 0; m <= M; m++) {
            if (n + m - 1 >= 0) precalc[n][m] = fac.ncr(n + m - 1, n - 1);
            if (m >= 2) precalc[n][m] += precalc[n][m - 2];
        }
    }

    mint ret = 0;
    if (N % 2 == 0) {
        int half = (N - 1) / 2;
        for (int nnonzero = 0; nnonzero <= half * 2; nnonzero++) {
            mint coeff = 0;
            for (int npos = 0; npos <= half; npos++) {
                int nneg = nnonzero - npos;
                if (nneg < 0 or nneg > half) continue;
                coeff += fac.ncr(N - 1, npos) * fac.ncr(N - 1 - npos, nneg);
            }
            mint tmp = 0;
            for (int b0 = 0; b0 <= M; b0++) {
                if (M - nnonzero - b0 < 0) continue;
                tmp += precalc.at(nnonzero).at(M - nnonzero - b0) * (b0 == 0 ? 1 : 2);
            }
            ret += tmp * coeff;
        }
    } else {
        for (int npos = 0; npos <= N / 2; npos++) {
            for (int nneg = 0; nneg <= N / 2; nneg++) {
                int nnonzero = npos + nneg;
                if (M - nnonzero < 0) continue;
                ret += fac.ncr(N, nnonzero) * fac.ncr(nnonzero, npos) * precalc[nnonzero][M - nnonzero];
            }
        }

        for (int npos = 0; npos <= N / 2; npos++) {
            for (int nneg = 0; nneg <= N / 2; nneg++) {
                int nnonzero = npos + nneg;
                int m = M - npos - (N - nneg);
                if (m < 0) continue;
                ret += fac.ncr(N, nnonzero) * fac.ncr(nnonzero, npos) * precalc[nnonzero][m] * 2;
            }
        }

        for (int npos = 0; npos <= N / 2; npos++) {
            for (int nneg = 0; nneg <= N / 2; nneg++) {
                const int nnonzero = npos + nneg;
                int m = M - (N - nnonzero) - max(npos, nneg) * 2;
                if (m < 0) continue;
                ret -= fac.ncr(N, nnonzero) * fac.ncr(nnonzero, npos) * precalc[nnonzero][m];
            }
        }
    }
    cout << ret.val() << '\n';
}
0