結果

問題 No.834 Random Walk Trip
ユーザー Shinya Fujita
提出日時 2025-01-11 02:58:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 197 ms / 2,000 ms
コード長 1,229 bytes
コンパイル時間 645 ms
コンパイル使用メモリ 82,564 KB
実行使用メモリ 161,664 KB
最終ジャッジ日時 2025-01-11 02:58:37
合計ジャッジ時間 4,097 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

N, M = map(int, input().split())
if N == 1:
    print(1)
    quit()

pos = []
data = [list(range(1, N+1)), list(range(N, 0, -1))]
idx = 0
while len(pos) < M+1:
    nex = data[idx].copy()
    nex = nex[:M+1 - len(pos)]
    pos += nex
    idx ^= 1

pos = pos + pos[:-1][::-1]


MOD = 10**9 + 7
num = 10**6 + 1
frac = [1] * num
inv_frac = [1] * num
for i in range(2, num):
    frac[i] = (frac[i-1] * i) % MOD

inv_frac[-1] = pow(frac[-1], MOD-2, MOD)
for i in range(num-1, 0, -1):
    inv_frac[i-1] = (inv_frac[i] * i) % MOD

def comb(n, r):
    if r < 0 or r > n:
        return 0
    
    return (frac[n] * (inv_frac[n-r] * inv_frac[r])%MOD) % MOD



ans = 0

if M%2 == 0:
    ans += comb(M, M//2)
    ans %= MOD

for x in range(1, M+1):
    if pos[x] == 1 and (x+M)%2 == 0:
        # M回操作して +xとなる
        # => 表をa回、裏をM-a回とすると、a - (M-a) = x <=> 2a = x+M <=> a = (x+M)//2
        a = (x + M) // 2
        ans += comb(M, a)
        ans %= MOD
    
    if pos[-x] == 1 and (M-x)%2 == 0:
        # M回操作して -xとなる
        # => 表をa回、裏をM-a回とすると、a-(M-a) = -x <=> a = (M-x) // 2
        a = (M - x) // 2
        ans += comb(M, a)
        ans %= MOD


print(ans)
0