結果

問題 No.823 Many Shifts Easy
ユーザー terasaterasa
提出日時 2022-11-03 11:44:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 170 ms / 2,000 ms
コード長 1,732 bytes
コンパイル時間 273 ms
コンパイル使用メモリ 86,644 KB
実行使用メモリ 95,248 KB
最終ジャッジ日時 2023-09-24 21:26:11
合計ジャッジ時間 2,622 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 103 ms
72,432 KB
testcase_01 AC 102 ms
72,416 KB
testcase_02 AC 114 ms
76,792 KB
testcase_03 AC 158 ms
92,544 KB
testcase_04 AC 103 ms
72,468 KB
testcase_05 AC 144 ms
87,696 KB
testcase_06 AC 156 ms
90,652 KB
testcase_07 AC 106 ms
72,628 KB
testcase_08 AC 170 ms
95,248 KB
testcase_09 AC 118 ms
77,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import itertools
import heapq
import bisect
from collections import deque, defaultdict
from functools import lru_cache, cmp_to_key

input = sys.stdin.readline

# for AtCoder Easy test
if __file__ != 'prog.py':
    sys.setrecursionlimit(10 ** 6)


def readints(): return map(int, input().split())
def readlist(): return list(readints())
def readstr(): return input().rstrip()


class Combination:
    def __init__(self, N, mod):
        self.N = N
        self.mod = mod
        self.f = [None] * (N + 1)
        self.finv = [None] * (N + 1)
        self.inv = [None] * (N + 1)

        self.f[0] = 1
        self.f[1] = 1
        self.finv[0] = 1
        self.finv[1] = 1
        self.inv[1] = 1
        for i in range(2, N + 1):
            self.f[i] = self.f[i - 1] * i % self.mod
            self.inv[i] = self.mod - self.inv[self.mod % i] * (self.mod // i) % self.mod
            self.finv[i] = self.finv[i - 1] * self.inv[i] % self.mod

    def P(self, n, k):
        if n < k:
            return 0
        if n < 0 or k < 0:
            return 0
        return self.f[n] * self.finv[n - k] % self.mod

    def C(self, n, k):
        if n < k:
            return 0
        if n < 0 or k < 0:
            return 0
        return self.f[n] * (self.finv[k] * self.finv[n - k] % self.mod) % self.mod

    # 重複組合せ
    # n種類のものからk個選ぶ
    def H(self, n, k):
        if n == 0 and k == 0:
            return 1
        return self.C(k + n - 1, k)


N, K = readints()
mod = 10 ** 9 + 7
comb = Combination(N, mod)

ans = 0
for i in range(N):
    ans += (i + 1) * comb.P(N - 1, K) % mod
    if i < N - 1:
        ans += (i + 1) * comb.P(N - 2, K - 2) * comb.C(K, 2) % mod
    ans %= mod
print(ans)
0