結果

問題 No.1685 One by One
ユーザー 👑 hitonanodehitonanode
提出日時 2021-08-14 02:30:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 979 ms / 3,000 ms
コード長 2,282 bytes
コンパイル時間 433 ms
コンパイル使用メモリ 87,124 KB
実行使用メモリ 203,752 KB
最終ジャッジ日時 2023-09-12 06:22:19
合計ジャッジ時間 19,755 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,312 KB
testcase_01 AC 74 ms
71,332 KB
testcase_02 AC 105 ms
77,204 KB
testcase_03 AC 213 ms
91,328 KB
testcase_04 AC 137 ms
82,732 KB
testcase_05 AC 550 ms
138,264 KB
testcase_06 AC 317 ms
114,024 KB
testcase_07 AC 397 ms
115,564 KB
testcase_08 AC 103 ms
76,884 KB
testcase_09 AC 132 ms
81,256 KB
testcase_10 AC 735 ms
181,140 KB
testcase_11 AC 139 ms
79,944 KB
testcase_12 AC 109 ms
77,148 KB
testcase_13 AC 458 ms
121,180 KB
testcase_14 AC 167 ms
86,324 KB
testcase_15 AC 720 ms
163,448 KB
testcase_16 AC 127 ms
81,868 KB
testcase_17 AC 121 ms
80,720 KB
testcase_18 AC 130 ms
81,488 KB
testcase_19 AC 93 ms
76,144 KB
testcase_20 AC 192 ms
77,424 KB
testcase_21 AC 206 ms
78,108 KB
testcase_22 AC 227 ms
83,448 KB
testcase_23 AC 252 ms
84,508 KB
testcase_24 AC 736 ms
182,996 KB
testcase_25 AC 913 ms
193,900 KB
testcase_26 AC 783 ms
191,280 KB
testcase_27 AC 860 ms
186,044 KB
testcase_28 AC 855 ms
203,388 KB
testcase_29 AC 979 ms
203,612 KB
testcase_30 AC 872 ms
203,688 KB
testcase_31 AC 964 ms
203,500 KB
testcase_32 AC 959 ms
203,752 KB
testcase_33 AC 969 ms
203,700 KB
testcase_34 AC 89 ms
76,148 KB
testcase_35 AC 90 ms
76,304 KB
testcase_36 AC 88 ms
76,624 KB
testcase_37 AC 87 ms
76,412 KB
testcase_38 AC 202 ms
76,912 KB
testcase_39 AC 194 ms
76,968 KB
testcase_40 AC 216 ms
78,080 KB
testcase_41 AC 218 ms
78,532 KB
testcase_42 AC 74 ms
71,200 KB
testcase_43 AC 74 ms
71,552 KB
testcase_44 AC 75 ms
71,272 KB
testcase_45 AC 74 ms
71,336 KB
testcase_46 AC 337 ms
113,860 KB
testcase_47 AC 331 ms
113,760 KB
testcase_48 AC 334 ms
113,652 KB
testcase_49 AC 331 ms
113,768 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import itertools
N, M = map(int, input().split())

class facmod:
    def __init__(self, N: int, md: int) -> None:
        assert N < md
        self.md = md
        self.facs = [1] * (N + 1)
        self.facinvs = [1] * (N + 1)
        for i in range(1, N + 1):
            self.facs[i] = self.facs[i - 1] * i % md
        self.facinvs[N] = pow(self.facs[i], md - 2, md)
        for i in range(N - 1, 0, -1):
            self.facinvs[i] = self.facinvs[i + 1] * (i + 1) % md

    def nCr(self, n: int, r: int) -> int:
        if n < 0 or r < 0 or n < r:
            return 0
        return (self.facs[n] * self.facinvs[r]) % md * self.facinvs[n - r] % md

md = 1000000007
fac = facmod(N + M, md)

precalc = [[0] * (M + 1) for _ in range(N + 1)]
precalc[0][0] = 1
for n in range(N + 1):
    for m in range(M + 1):
        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]
            if precalc[n][m] >= md:
                precalc[n][m] -= md

ret = 0

if N % 2 == 0:
    half = (N - 1) // 2
    for nnonzero in range(half * 2 + 1):
        coeff = 0
        for npos in range(half + 1):
            if nnonzero - npos < 0 or nnonzero - npos > half:
                continue
            coeff += fac.nCr(N - 1, npos) * fac.nCr(N - 1 - npos, nnonzero - npos)
            coeff %= md
        tmp = 0
        for b0 in range(M + 1):
            if M - nnonzero - b0 < 0:
                break
            tmp += precalc[nnonzero][M - nnonzero - b0] * (2 - (b0 == 0))
        ret += tmp % md * coeff
        ret %= md
else:
    for npos in range(N // 2 + 1):
        for nneg in range(N // 2 + 1):
            nnonzero = npos + nneg
            coeff = 0
            if M - (npos + nneg) >= 0:
                coeff += precalc[nnonzero][M - nnonzero]
            m = M - npos - (N - nneg)
            if m >= 0:
                coeff += precalc[nnonzero][m] * 2
            m = M - (N - nnonzero) - (npos if npos > nneg else nneg) * 2
            if m >= 0:
                coeff += md - precalc[nnonzero][m]
            ret += fac.nCr(N, nnonzero) * fac.nCr(nnonzero, npos) % md * (coeff % md)
            ret %= md

print(ret)
0