結果

問題 No.940 ワープ ε=ε=ε=ε=ε=│;p>д<│
ユーザー maspymaspy
提出日時 2019-12-03 04:14:35
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,365 ms / 5,000 ms
コード長 3,095 bytes
コンパイル時間 128 ms
コンパイル使用メモリ 11,216 KB
実行使用メモリ 174,960 KB
最終ジャッジ日時 2023-08-18 23:27:49
合計ジャッジ時間 17,347 ms
ジャッジサーバーID
(参考情報)
judge10 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 387 ms
125,016 KB
testcase_01 AC 387 ms
125,160 KB
testcase_02 AC 389 ms
125,012 KB
testcase_03 AC 380 ms
125,160 KB
testcase_04 AC 383 ms
125,060 KB
testcase_05 AC 383 ms
125,296 KB
testcase_06 AC 385 ms
125,204 KB
testcase_07 AC 379 ms
125,084 KB
testcase_08 AC 380 ms
125,052 KB
testcase_09 AC 385 ms
125,016 KB
testcase_10 AC 381 ms
125,044 KB
testcase_11 AC 384 ms
125,032 KB
testcase_12 AC 375 ms
125,056 KB
testcase_13 AC 384 ms
125,116 KB
testcase_14 AC 385 ms
125,176 KB
testcase_15 AC 575 ms
125,100 KB
testcase_16 AC 1,209 ms
158,348 KB
testcase_17 AC 421 ms
125,028 KB
testcase_18 AC 428 ms
125,256 KB
testcase_19 AC 427 ms
125,176 KB
testcase_20 AC 443 ms
125,212 KB
testcase_21 AC 403 ms
125,208 KB
testcase_22 AC 823 ms
142,256 KB
testcase_23 AC 1,205 ms
158,896 KB
testcase_24 AC 874 ms
146,308 KB
testcase_25 AC 1,284 ms
168,656 KB
testcase_26 AC 1,365 ms
174,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np

X,Y,Z = map(int,read().split())

MOD = 10 ** 9 + 7

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

def make_fact(U,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]
    return fact,fact_inv

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

U = 2 * 10 ** 6 + 100
fact,fact_inv = make_fact(U,MOD)
power2 = make_power(2,U,MOD)

def convolve(A,B,size=32):
    """
    誤差回避のため8bit整数以下にしてからnp.fftを呼ぶ
    """
    if size <= 8:
        LA = len(A); LB = len(B); L = LA + LB - 1
        fft_len = 1 << (L.bit_length())
        ifft = np.fft.irfft; fft = np.fft.rfft
        return ((ifft(fft(A,fft_len) * fft(B,fft_len))) + .5).astype(np.int64)[:L] % MOD
    size //= 2
    M = 1 << size
    A1,A2 = np.divmod(A,M) # yukiのバージョンならあるはず
    B1,B2 = np.divmod(B,M)
    X = convolve(A1,B1,size)
    Y = convolve(A1+A2,B1+B2,size)
    Z = convolve(A2,B2,size)
    return ((X * (M * M % MOD)) + (Y - X - Z) * M + Z) % MOD

def f(X,Y,Z):
    """
    ・(2 - (1-x)^{-1}(1-y)^{-1}(1-z)^{-1})^{-1} の係数を求める問題
    ・P = x + y + xy
    ・(1-P)(1-z)/((1-2P)-(2-2P)z) の係数を求める問題
    ・g(n) = (1-P)/((1-2P)-(2-2P)z) の x^Xy^Yz^n の係数として、g(n) - g(n-1) を求める
    """
    return (g(X,Y,Z) - g(X,Y,Z-1)) % MOD

def g(X,Y,N):
    """
    (1-P)/((1-2P)-(2-2P)z) の x^Xy^Yz^N の係数を返す
    """
    if N < 0:
        return 0
    return pow(2,N,MOD) * h(X,Y,N+1) % MOD

def h(X,Y,N):
    """
    (1-P)^N / (1-2P)^N の x^Xy^Y の係数を返す
    """
    U = X + Y + 100
    # 分子
    A = fact[N] * fact_inv[:N+1] % MOD * fact_inv[:N+1][::-1]
    A[1::2] *= (-1); A %= MOD
    A = A[:U]
    # 分母の逆
    B = fact[N-1:N+U-1] * fact_inv[N-1] % MOD * fact_inv[:U] % MOD
    B *= power2[:U]; B %= MOD
    C = convolve(A,B)[:U]
    # 各 n に対して、P^n = (x + y - xy)^n での x^Xy^Y の係数を求める
    L = max(X,Y); R = X+Y
    coef = np.zeros(U,np.int64)
    coef[L:R+1] = fact[L:R+1]
    coef[L:R+1] *= fact_inv[L-Y:R-Y+1]; coef[L:R+1] %= MOD
    coef[L:R+1] *= fact_inv[L-X:R-X+1]; coef[L:R+1] %= MOD
    coef[L:R+1] *= fact_inv[X+Y-R:X+Y-L+1][::-1]; coef[L:R+1] %= MOD
    coef[(X+Y+1)%2::2] *= (-1)
    return (coef * C % MOD).sum() % MOD

# 3, 13, 512, 882313923
# f(1,1,0), f(1,1,1), f(10,0,0), f(31,53,6000), f(53,31,6000)
# f(6000,31,53), f(6000,53,31)

answer = f(X,Y,Z)
print(answer)
0