結果

問題 No.502 階乗を計算するだけ
ユーザー Kiri8128Kiri8128
提出日時 2020-09-15 00:54:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 946 ms / 1,000 ms
コード長 2,132 bytes
コンパイル時間 354 ms
コンパイル使用メモリ 82,376 KB
実行使用メモリ 132,804 KB
最終ジャッジ日時 2024-06-22 01:02:46
合計ジャッジ時間 11,967 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
52,960 KB
testcase_01 AC 38 ms
53,624 KB
testcase_02 AC 39 ms
53,304 KB
testcase_03 AC 39 ms
53,104 KB
testcase_04 AC 40 ms
53,504 KB
testcase_05 AC 40 ms
53,544 KB
testcase_06 AC 40 ms
53,380 KB
testcase_07 AC 38 ms
53,316 KB
testcase_08 AC 37 ms
53,320 KB
testcase_09 AC 39 ms
53,052 KB
testcase_10 AC 38 ms
53,472 KB
testcase_11 AC 39 ms
53,212 KB
testcase_12 AC 39 ms
54,416 KB
testcase_13 AC 38 ms
54,044 KB
testcase_14 AC 38 ms
53,124 KB
testcase_15 AC 39 ms
52,884 KB
testcase_16 AC 39 ms
53,592 KB
testcase_17 AC 41 ms
52,944 KB
testcase_18 AC 39 ms
53,376 KB
testcase_19 AC 39 ms
53,808 KB
testcase_20 AC 38 ms
53,592 KB
testcase_21 AC 40 ms
54,544 KB
testcase_22 AC 79 ms
76,804 KB
testcase_23 AC 61 ms
70,280 KB
testcase_24 AC 79 ms
76,740 KB
testcase_25 AC 61 ms
70,944 KB
testcase_26 AC 76 ms
76,844 KB
testcase_27 AC 60 ms
70,800 KB
testcase_28 AC 76 ms
76,764 KB
testcase_29 AC 59 ms
70,488 KB
testcase_30 AC 77 ms
76,764 KB
testcase_31 AC 77 ms
76,684 KB
testcase_32 AC 932 ms
132,672 KB
testcase_33 AC 936 ms
132,792 KB
testcase_34 AC 935 ms
132,664 KB
testcase_35 AC 402 ms
94,044 KB
testcase_36 AC 932 ms
132,668 KB
testcase_37 AC 944 ms
132,668 KB
testcase_38 AC 936 ms
132,668 KB
testcase_39 AC 946 ms
132,668 KB
testcase_40 AC 400 ms
94,044 KB
testcase_41 AC 933 ms
132,804 KB
testcase_42 AC 39 ms
53,960 KB
testcase_43 AC 38 ms
54,260 KB
testcase_44 AC 38 ms
54,232 KB
testcase_45 AC 39 ms
53,688 KB
testcase_46 AC 38 ms
53,692 KB
testcase_47 AC 38 ms
53,656 KB
testcase_48 AC 39 ms
53,840 KB
testcase_49 AC 39 ms
53,536 KB
testcase_50 AC 38 ms
54,044 KB
testcase_51 AC 37 ms
52,744 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

k = 72
kk = k // 4
K = 1<<k
nu = lambda L: int("".join([hex(K+a)[3:] for a in L[::-1]]), 16)
st = lambda n: hex(n)[2:]
li = lambda s, l, r: [int(a, 16) % P if len(a) else 0 for a in [s[-(i+1)*kk:-i*kk] for i in range(l, r)]]

def grow(d, v, h):
    h += [0] * d
    f = [(-1 if (i+d) % 2 else 1) * fainv[i] * fainv[d-i] % P * h[i] % P for i in range(d+1)]
    nuf = nu(f)
    a = d * inv[v] % P
    t = [1] * (3*d+3)
    for i in range(1, 3*d+3): t[i] = t[i-1] * (a - d + i - 1) % P
    ti = [1] * (3*d+3)
    ti[-1] = pow(t[-1], P-2, P)
    for i in range(1, 3*d+3)[::-1]: ti[i-1] = ti[i] * (a - d + i - 1) % P
    iv = [1] * (3*d+3)
    for i in range(1, 3*d+3):
        iv[i] = ti[i] * t[i-1] % P

    ###
    g = [inv[i] for i in range(1, 2*d+2)]
    fg = li(st(nuf * nu(g)), d, d * 2 + 1)
    for i in range(d):
        h[i+d+1] = fg[i] * fa[d+i+1] % P * fainv[i] % P

    ###
    g = [iv[i] for i in range(1, 2*d+2)]
    fg = li(st(nuf * nu(g)), d, d * 2 + 1)
    for i in range(d+1):
        h[i] = h[i] * (fg[i] * t[d+i+1] % P * ti[i] % P) % P

    ###
    g = [iv[i] for i in range(d+2, 3*d+3)]
    fg = li(st(nuf * nu(g)), d, d * 2 + 1)
    for i in range(d):
        h[i+d+1] = h[i+d+1] * (fg[i] * t[2*d+i+2] % P * ti[d+i+1] % P) % P

    return h

# Create a table of the factorial of the first v+2 multiples of v, i.e., [0!, v!, 2v!, ..., (v(v+1))!]
def create_table(v):
    s = 1
    X = [1, v+1]
    while s < v:
        X = grow(s, v, X)
        s *= 2

    table = [1]
    for x in X:
        table.append(table[-1] * x % P)
    return table

def fact(i, table):
    a = table[i//v]
    for j in range(i//v*v+1, i+1):
        a = a * j % P
    return a

P = 10**9+7
N = int(input())
if N >= P:
    print(0)
else:
    v = 1 << (N.bit_length() + 1) // 2
    fa = [1] * (2*v+2)
    fainv = [1] * (2*v+2)
    for i in range(2*v+1):
        fa[i+1] = fa[i] * (i+1) % P
    fainv[-1] = pow(fa[-1], P-2, P)
    for i in range(2*v+1)[::-1]:
        fainv[i] = fainv[i+1] * (i+1) % P
    inv = [0] * (2*v+2)
    for i in range(1, 2*v+2):
        inv[i] = fainv[i] * fa[i-1] % P

    T = create_table(v)
    print(fact(N, T))
0