結果

問題 No.214 素数サイコロと合成数サイコロ (3-Medium)
ユーザー maspymaspy
提出日時 2020-05-09 01:51:16
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 233 ms / 3,000 ms
コード長 2,040 bytes
コンパイル時間 85 ms
コンパイル使用メモリ 10,904 KB
実行使用メモリ 30,576 KB
最終ジャッジ日時 2023-09-17 06:42:15
合計ジャッジ時間 1,742 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 233 ms
30,444 KB
testcase_01 AC 230 ms
30,576 KB
testcase_02 AC 233 ms
30,416 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import numpy as np

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

MOD = 10**9 + 7

def fft_convolve(f, g, MOD=MOD):
    """
    数列 (多項式) f, g の畳み込みの計算.上下 15 bitずつ分けて計算することで,
    30 bit以下の整数,長さ 250000 程度の数列での計算が正確に行える.
    """
    fft = np.fft.rfft
    ifft = np.fft.irfft
    Lf = len(f)
    Lg = len(g)
    L = Lf + Lg - 1
    fft_len = 1 << L.bit_length()
    fl = f & (1 << 15) - 1
    fh = f >> 15
    gl = g & (1 << 15) - 1
    gh = g >> 15

    def conv(f, g):
        return ifft(fft(f, fft_len) * fft(g, fft_len))[:L]

    x = conv(fl, gl) % MOD
    y = conv(fl + fh, gl + gh) % MOD
    z = conv(fh, gh) % MOD
    a, b, c = map(lambda x: (x + .5).astype(np.int64), [x, y, z])
    return (a + ((b - a - c) << 15) + (c << 30)) % MOD


def coef_of_generating_function(P, Q, N):
    """compute the coefficient [x^N] P/Q of rational power series.

    Parameters
    ----------
    P : np.ndarray
        numerator.
    Q : np.ndarray
        denominator
        Q[0] == 1 and len(Q) == len(P) + 1 is assumed.
    N : int
        The coefficient to compute.
    """
    def convolve(f, g):
        return fft_convolve(f, g, MOD)

    while N:
        Q1 = Q.copy()
        Q1[1::2] = np.negative(Q1[1::2])
        if N & 1:
            P = convolve(P, Q1)[1::2]
        else:
            P = convolve(P, Q1)[::2]
        Q = convolve(Q, Q1)[::2]
        N >>= 1
    return P[0]

N, P, C = map(int, read().split())

def dice(nums, n_dice):
    U = nums[-1] * n_dice
    dp = np.zeros((n_dice + 1, U + 1), np.int64)
    dp[0, 0] = 1
    for x in nums:
        for i in range(1, n_dice + 1):
            dp[i, x:] += dp[i - 1, :-x]
    return dp[-1]

f1 = dice((2, 3, 5, 7, 11, 13), P)
f2 = dice((4, 6, 8, 9, 10, 12), C)
f = np.convolve(f1, f2) % MOD

den = -f
den[0] += 1
g = f[::-1].cumsum()[::-1] % MOD
g[0] = 0

print(coef_of_generating_function(g, den, N))
0