結果

問題 No.766 金魚すくい
ユーザー maspy
提出日時 2020-02-04 09:45:11
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 620 ms / 1,500 ms
コード長 1,555 bytes
コンパイル時間 209 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 54,924 KB
最終ジャッジ日時 2024-09-19 20:29:53
合計ジャッジ時間 26,050 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

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


def make_power(a, L, MOD=MOD):
    B = L.bit_length()
    x = np.empty((1<<B), np.int64)
    x[0] = 1
    for n in range(B):
        x[1<<n:1<<(n+1)] = x[:1<<n] * a % MOD
        a *= a; a %= MOD
    x = x[:L]
    x.flags.writeable = False
    return x

N, M, P = map(int, readline().split())
V = np.fromstring(read(), np.int64, sep=' ')

fact, fact_inv = make_fact(N + M)
P *= pow(100, MOD-2, MOD)
Q = 1 - P
P %= MOD
Q %= MOD

power_Q = make_power(Q, N+M)

comb = fact[M-1:N+M-1] * fact_inv[0:N] % MOD * fact_inv[M-1] % MOD
prob = np.empty(N+1, dtype=np.int64)
prob[:N] = comb * power_Q[:N] % MOD * pow(P, M, MOD) % MOD

prob[-1] = (1 - prob[:-1].sum()) % MOD

V.sort()
V = V[::-1]
Vcum = np.empty(N+1, np.int64)
Vcum[0] = 0
Vcum[1:] = V.cumsum() % MOD

answer = (Vcum * prob % MOD).sum() % MOD
print(answer)
0