結果

問題 No.916 Encounter On A Tree
ユーザー maspy
提出日時 2020-02-27 14:50:25
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 582 ms / 2,000 ms
コード長 1,420 bytes
コンパイル時間 85 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 85,148 KB
最終ジャッジ日時 2024-10-13 15:57:17
合計ジャッジ時間 34,204 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 56
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
# %%
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


# %%
import numpy as np

# %%
MOD = 10 ** 9 + 7

# %%


def cumprod(A, MOD=MOD):
    L = len(A)
    Lsq = int(L**.5 + 1)
    A = np.resize(A, Lsq**2).reshape(Lsq, Lsq)
    for n in range(1, Lsq):
        A[:, n] *= A[:, n - 1]
        A[:, n] %= MOD
    for n in range(1, Lsq):
        A[n] *= A[n - 1, -1]
        A[n] %= MOD
    return A.ravel()[:L]


def make_fact(U, MOD=MOD):
    x = np.arange(U, dtype=np.int64)
    x[0] = 1
    fact = cumprod(x, MOD)
    x = np.arange(U, 0, -1, dtype=np.int64)
    x[0] = pow(int(fact[-1]), MOD - 2, MOD)
    fact_inv = cumprod(x, MOD)[::-1]
    fact.flags.writeable = False
    fact_inv.flags.writeable = False
    return fact, fact_inv


# %%
D, L, R, K = map(int, read().split())

# %%
DL = L.bit_length()
DR = R.bit_length()
if DL > DR:
    DL, DR = DL, DR
lca_dep, r = divmod((DL + DR - K), 2)
if lca_dep <= 0 or lca_dep > L or r:
    print(0)
    exit()

# %%
K, DL, DR, lca_dep
if lca_dep == DL:
    R_cand = 2 ** (DR - DL)
else:
    R_cand = 2 ** (DR - lca_dep - 1)
# %%
fact, fact_inv = make_fact(2 ** 20)
total = 1
for n in range(1, D + 1):
    total *= fact[2 ** (n - 1)]
    total %= MOD
total *= R_cand
total %= MOD
d = pow(2, DR - 1, MOD)
if DL == DR:
    d -= 1
total *= pow(d, MOD - 2, MOD)
total %= MOD
print(total)
0