結果

問題 No.502 階乗を計算するだけ
ユーザー Kiri8128Kiri8128
提出日時 2020-02-25 03:09:05
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,856 bytes
コンパイル時間 168 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 34,808 KB
最終ジャッジ日時 2024-04-21 06:01:31
合計ジャッジ時間 4,837 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
16,384 KB
testcase_01 AC 32 ms
11,008 KB
testcase_02 AC 31 ms
11,008 KB
testcase_03 AC 31 ms
11,136 KB
testcase_04 AC 31 ms
11,008 KB
testcase_05 AC 31 ms
11,008 KB
testcase_06 AC 31 ms
11,008 KB
testcase_07 AC 32 ms
10,880 KB
testcase_08 AC 31 ms
11,008 KB
testcase_09 AC 31 ms
10,880 KB
testcase_10 AC 32 ms
11,008 KB
testcase_11 AC 31 ms
11,008 KB
testcase_12 AC 31 ms
11,008 KB
testcase_13 AC 31 ms
11,008 KB
testcase_14 AC 31 ms
11,008 KB
testcase_15 AC 32 ms
11,008 KB
testcase_16 AC 32 ms
11,008 KB
testcase_17 AC 32 ms
10,880 KB
testcase_18 AC 32 ms
11,008 KB
testcase_19 AC 32 ms
11,008 KB
testcase_20 AC 32 ms
10,880 KB
testcase_21 AC 33 ms
11,008 KB
testcase_22 AC 108 ms
11,904 KB
testcase_23 AC 65 ms
11,520 KB
testcase_24 AC 100 ms
11,776 KB
testcase_25 AC 65 ms
11,520 KB
testcase_26 AC 101 ms
11,904 KB
testcase_27 AC 65 ms
11,520 KB
testcase_28 AC 100 ms
11,904 KB
testcase_29 AC 66 ms
11,648 KB
testcase_30 AC 100 ms
11,904 KB
testcase_31 AC 102 ms
11,904 KB
testcase_32 TLE -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

k = 74
K = 1<<k
nu = lambda L: int("".join([bin(K+a)[-k:] for a in L[::-1]]), 2)
st = lambda n: bin(n)[2:] + "0"
li = lambda s, l: [int(a, 2) if len(a) else 0 for a in [s[-(i+1)*k-1:-i*k-1] for i in range(l)]]

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)]
    
    for idx, a in enumerate([d+1, d * fa[v-1] * fainv[v] % P, (d * fa[v-1] * fainv[v] + d + 1) % P]):
        g = [pow(a - d + i - 1, P-2, P) if i else 0 for i in range(2*d+2)]
        fg = li(st(nu(f) * nu(g)), d * 8 - 1)
        p = 1
        for i in range(d+1):
            p = p * (a-i) % P
        for i in range(d+1):
            fg[d+i+1] = fg[d+i+1] * p % P
            p = p * (a+i+1) % P * pow(a-d+i, P-2, P) % P
        if idx == 1:
            for i in range(d+1):
                h[i] = h[i] * fg[d+i+1] % P
        elif idx == 0:
            for i in range(d):
                h[i+d+1] = fg[d+i+1]
        elif idx == 2:
            for i in range(d):
                h[i+d+1] = h[i+d+1] * fg[d+i+1] % 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

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