結果

問題 No.834 Random Walk Trip
ユーザー qwewe
提出日時 2025-05-14 12:55:35
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,235 bytes
コンパイル時間 163 ms
コンパイル使用メモリ 82,292 KB
実行使用メモリ 77,812 KB
最終ジャッジ日時 2025-05-14 12:56:19
合計ジャッジ時間 5,677 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 17 TLE * 1 -- * 8
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10**9 + 7

N, M = map(int, input().split())

if N == 1:
    print(1)
elif N >= M:
    # Compute C(M, M//2) mod MOD using precomputed factorials and inverse factorials
    max_n = M
    fact = [1] * (max_n + 1)
    for i in range(1, max_n + 1):
        fact[i] = fact[i-1] * i % MOD
    inv_fact = [1] * (max_n + 1)
    inv_fact[max_n] = pow(fact[max_n], MOD-2, MOD)
    for i in range(max_n - 1, -1, -1):
        inv_fact[i] = inv_fact[i+1] * (i+1) % MOD
    k = M // 2
    ans = fact[M] * inv_fact[k] % MOD
    ans = ans * inv_fact[M - k] % MOD
    print(ans)
else:
    # Use dynamic programming for small N
    dp = [0] * (N + 2)  # 1-based indexing
    dp[1] = 1
    for _ in range(M):
        new_dp = [0] * (N + 2)
        for x in range(1, N + 1):
            if x == 1:
                new_dp[1] = (new_dp[1] + dp[x]) % MOD
                if N >= 2:
                    new_dp[2] = (new_dp[2] + dp[x]) % MOD
            elif x == N:
                new_dp[N-1] = (new_dp[N-1] + dp[x]) % MOD
                new_dp[N] = (new_dp[N] + dp[x]) % MOD
            else:
                new_dp[x-1] = (new_dp[x-1] + dp[x]) % MOD
                new_dp[x+1] = (new_dp[x+1] + dp[x]) % MOD
        dp = new_dp
    print(dp[1] % MOD)
0