結果

問題 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  
実行時間 828 ms / 2,000 ms
コード長 2,297 bytes
コンパイル時間 84 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 49,976 KB
最終ジャッジ日時 2024-10-01 07:02:27
合計ジャッジ時間 7,261 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
10,752 KB
testcase_01 AC 31 ms
10,752 KB
testcase_02 AC 33 ms
10,752 KB
testcase_03 AC 32 ms
10,752 KB
testcase_04 AC 31 ms
10,880 KB
testcase_05 AC 31 ms
10,752 KB
testcase_06 AC 32 ms
10,752 KB
testcase_07 AC 32 ms
10,880 KB
testcase_08 AC 31 ms
10,752 KB
testcase_09 AC 828 ms
49,976 KB
testcase_10 AC 707 ms
49,836 KB
testcase_11 AC 591 ms
49,860 KB
testcase_12 AC 30 ms
10,752 KB
testcase_13 AC 776 ms
49,708 KB
testcase_14 AC 641 ms
34,820 KB
testcase_15 AC 732 ms
36,756 KB
testcase_16 AC 444 ms
30,092 KB
testcase_17 AC 511 ms
32,208 KB
testcase_18 AC 353 ms
21,848 KB
testcase_19 AC 359 ms
22,448 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