結果

問題 No.391 CODING WAR
ユーザー taq225taq225
提出日時 2020-03-09 00:46:57
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 613 ms / 2,000 ms
コード長 2,177 bytes
コンパイル時間 265 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 22,624 KB
最終ジャッジ日時 2024-04-25 09:46:27
合計ジャッジ時間 5,459 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
11,008 KB
testcase_01 AC 27 ms
10,880 KB
testcase_02 AC 26 ms
10,752 KB
testcase_03 AC 27 ms
11,008 KB
testcase_04 AC 28 ms
10,880 KB
testcase_05 AC 29 ms
10,880 KB
testcase_06 AC 25 ms
10,880 KB
testcase_07 AC 26 ms
11,008 KB
testcase_08 AC 25 ms
10,880 KB
testcase_09 AC 613 ms
22,624 KB
testcase_10 AC 510 ms
22,584 KB
testcase_11 AC 415 ms
22,584 KB
testcase_12 AC 26 ms
10,880 KB
testcase_13 AC 586 ms
22,484 KB
testcase_14 AC 488 ms
19,796 KB
testcase_15 AC 551 ms
20,864 KB
testcase_16 AC 353 ms
16,576 KB
testcase_17 AC 389 ms
17,784 KB
testcase_18 AC 286 ms
15,392 KB
testcase_19 AC 283 ms
15,544 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 = [0] * (N+1)           # n!
        self.inverse = [None] + [0] * N   # inverse of n in the field Z/(MOD)Z
        self.fact_inverse = [0] * (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