結果

問題 No.391 CODING WAR
ユーザー Takahiro INOUETakahiro INOUE
提出日時 2019-06-06 18:25:17
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 682 ms / 2,000 ms
コード長 2,297 bytes
コンパイル時間 100 ms
コンパイル使用メモリ 12,032 KB
実行使用メモリ 49,340 KB
最終ジャッジ日時 2024-04-08 14:09:23
合計ジャッジ時間 6,452 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
10,240 KB
testcase_01 AC 30 ms
10,240 KB
testcase_02 AC 30 ms
10,240 KB
testcase_03 AC 30 ms
10,240 KB
testcase_04 AC 30 ms
10,240 KB
testcase_05 AC 30 ms
10,240 KB
testcase_06 AC 30 ms
10,240 KB
testcase_07 AC 32 ms
10,240 KB
testcase_08 AC 29 ms
10,240 KB
testcase_09 AC 682 ms
49,340 KB
testcase_10 AC 586 ms
49,340 KB
testcase_11 AC 507 ms
49,340 KB
testcase_12 AC 29 ms
10,240 KB
testcase_13 AC 645 ms
49,324 KB
testcase_14 AC 533 ms
34,312 KB
testcase_15 AC 599 ms
36,444 KB
testcase_16 AC 369 ms
29,428 KB
testcase_17 AC 425 ms
31,604 KB
testcase_18 AC 294 ms
21,440 KB
testcase_19 AC 302 ms
21,792 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
    
class Combinatorics:
    def __init__(self, N, mod):
        '''
        Preprocess for calculating binomial coefficients nCr (0 <= r <= n, 0 <= n <= N)
        over the finite field Z/(mod)Z.
        Input:
            N (int): maximum n
            mod (int): a prime number. The order of the field Z/(mod)Z over which nCr is calculated.
        '''
        self.mod = mod
        self.fact = {i: None for i in range(N+1)}         # n!
        self.inverse = {i: None for i in range(1, N+1)}   # inverse of n in the field Z/(MOD)Z
        self.fact_inverse = {i: None for i in range(N+1)} # inverse of n! in the field Z/(MOD)Z
        
        # preprocess
        self.fact[0] = self.fact[1] = 1
        self.fact_inverse[0] = self.fact_inverse[1] = 1
        self.inverse[1] = 1
        for i in range(2, N+1):
            self.fact[i] = i * self.fact[i-1] % self.mod
            q, r = divmod(self.mod, i)
            self.inverse[i] = (- (q % self.mod) * self.inverse[r]) % self.mod
            self.fact_inverse[i] = self.inverse[i] * self.fact_inverse[i-1] % self.mod
    
    def perm(self, n, r):
        '''
        Calculate nPr = n! / (n-r)! % mod
        '''
        if n < r or n < 0 or r < 0:
            return 0
        else:
            return (self.fact[n] * self.fact_inverse[n-r]) % self.mod
    
    def binom(self, n, r):
        '''
        Calculate nCr = n! /(r! (n-r)!) % mod
        '''
        if n < r or n < 0 or r < 0:
            return 0
        else:
            return self.fact[n] * (self.fact_inverse[r] * self.fact_inverse[n-r] % self.mod) % self.mod
        
    def hom(self, n, r):
        '''
        Calculate nHr = {n+r-1}Cr % mod.
        Assign r objects to one of n classes.
        Arrangement of r circles and n-1 partitions:
            o o o | o o | | | o | | | o o | | o
        '''
        if n == 0 and r > 0:
            return 0
        if n >= 0 and r == 0:
            return 1
        return self.binom(n + r - 1, r)
    
N, M = map(int, input().split())
MOD = 10**9 + 7
com = Combinatorics(M, MOD)
ans = 0
for i in range(M):
    if i % 2 == 0:
        ans = (ans + com.binom(M, i) * pow(M - i, N, MOD)) % MOD
    else:
        ans = (ans - com.binom(M, i) * pow(M - i, N, MOD)) % MOD
print(ans)
0