結果

問題 No.1685 One by One
ユーザー 👑 hitonanodehitonanode
提出日時 2021-08-14 12:11:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 328 ms / 3,000 ms
コード長 3,127 bytes
コンパイル時間 6,923 ms
コンパイル使用メモリ 221,292 KB
実行使用メモリ 66,236 KB
最終ジャッジ日時 2023-09-12 06:23:03
合計ジャッジ時間 12,459 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 36 ms
12,832 KB
testcase_04 AC 11 ms
5,928 KB
testcase_05 AC 182 ms
33,500 KB
testcase_06 AC 65 ms
21,356 KB
testcase_07 AC 78 ms
22,012 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 11 ms
5,628 KB
testcase_10 AC 203 ms
54,696 KB
testcase_11 AC 7 ms
4,872 KB
testcase_12 AC 4 ms
4,376 KB
testcase_13 AC 82 ms
24,964 KB
testcase_14 AC 24 ms
7,876 KB
testcase_15 AC 255 ms
45,908 KB
testcase_16 AC 9 ms
5,340 KB
testcase_17 AC 6 ms
4,800 KB
testcase_18 AC 9 ms
5,400 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 43 ms
4,380 KB
testcase_21 AC 12 ms
4,380 KB
testcase_22 AC 53 ms
6,556 KB
testcase_23 AC 20 ms
6,672 KB
testcase_24 AC 204 ms
56,028 KB
testcase_25 AC 292 ms
60,948 KB
testcase_26 AC 217 ms
60,240 KB
testcase_27 AC 289 ms
57,248 KB
testcase_28 AC 242 ms
66,224 KB
testcase_29 AC 327 ms
66,236 KB
testcase_30 AC 240 ms
66,048 KB
testcase_31 AC 324 ms
66,012 KB
testcase_32 AC 328 ms
66,096 KB
testcase_33 AC 323 ms
65,736 KB
testcase_34 AC 2 ms
4,384 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 3 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 50 ms
4,376 KB
testcase_39 AC 50 ms
4,376 KB
testcase_40 AC 14 ms
4,376 KB
testcase_41 AC 13 ms
4,376 KB
testcase_42 AC 2 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 71 ms
23,464 KB
testcase_47 AC 83 ms
23,232 KB
testcase_48 AC 71 ms
23,340 KB
testcase_49 AC 84 ms
23,468 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// Validator
#include "testlib.h"
#include <atcoder/modint>
#include <cassert>
#include <iostream>
#include <vector>
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.at(i) = facs.at(i - 1) * i;
        facinvs.assign(N + 1, facs.back().inv());
        for (int i = N; i > 0; i--) facinvs.at(i - 1) = facinvs.at(i) * i;
    }
    modint ncr(int n, int r) const {
        if (n < 0 or r < 0 or n < r) return 0;
        return facs.at(n) * facinvs[r] * facinvs[n - r];
    }
};

int main(int argc, char **argv) {
    registerValidation(argc, argv);
    int N = inf.readInt(2, 4000);
    inf.readSpace();
    int M = inf.readInt(0, 4000);
    inf.readEoln();
    inf.readEof();

    acl_fac<mint> fac(N + M);
    vector precalc(N + 1, vector<mint>(M + 1));
    precalc.at(0).at(0) = 1;
    for (int n = 0; n <= N; n++) {
        for (int m = 0; m <= M; m++) {
            if (n + m - 1 >= 0) precalc.at(n).at(m) = fac.ncr(n + m - 1, n - 1);
            if (m >= 2) precalc.at(n).at(m) += precalc.at(n).at(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.at(nnonzero).at(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.at(nnonzero).at(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.at(nnonzero).at(m);
            }
        }
    }
    cout << ret.val() << '\n';
}
0