結果

問題 No.1520 Zigzag Sum
ユーザー NatsubiSoganNatsubiSogan
提出日時 2021-02-03 22:40:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 340 ms / 2,000 ms
コード長 1,271 bytes
コンパイル時間 161 ms
コンパイル使用メモリ 81,780 KB
実行使用メモリ 82,460 KB
最終ジャッジ日時 2023-10-26 03:38:45
合計ジャッジ時間 3,175 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
65,592 KB
testcase_01 AC 54 ms
65,592 KB
testcase_02 AC 303 ms
82,460 KB
testcase_03 AC 339 ms
82,456 KB
testcase_04 AC 340 ms
82,456 KB
testcase_05 AC 339 ms
82,456 KB
testcase_06 AC 319 ms
82,456 KB
testcase_07 AC 313 ms
82,236 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 10 ** 9 + 7

#順列
class Permutation:

    #前計算
    def __init__(self, n):
        self.n = n
        self.fact = [1] * (self.n + 1)
        self.fact_inv = [1] * (self.n + 1)

        for i in range(1, self.n + 1):
            self.fact[i] = self.fact[i - 1] * i % mod

        self.fact_inv[-1] = invmod(self.fact[-1], mod)

        for i in range(self.n, 0, -1):
            self.fact_inv[i - 1] = self.fact_inv[i] * i % mod

    def perm(self, n, r):
        if n < r:return 0
        if n < 0 or r < 0:return 0
        return self.fact[n] * self.fact_inv[n - r] % mod

#拡張ユークリッドの互除法
def extgcd(a, b, d = 0):
    g = a
    if b == 0:
        x, y = 1, 0
    else:
        x, y, g = extgcd(b, a % b)
        x, y = y, x - a // b * y
    return x, y, g

#mod pにおける逆元
def invmod(a, p):
    x, y, g = extgcd(a, p)
    x %= p
    return x

hwmax = 4 * 10 ** 5

P = Permutation(hwmax)

t = int(input())
assert(1 <= t <= 2 * 10 ** 5)
cases = 0
for _ in range(t):
    h, w = map(int, input().split())
    cases += 1
    assert(1 <= h <= 2 * 10 ** 5 and 1 <= w <= 2 * 10 ** 5)
    if h == 1 or w == 1:
        print(0)
        continue
    print(2 * P.perm(h + w - 3, h - 1) % mod * P.fact_inv[h - 2] % mod)
assert(cases == t)
0