結果

問題 No.1491 銀将
ユーザー Kiri8128Kiri8128
提出日時 2021-04-30 21:46:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 56 ms / 1,000 ms
コード長 2,891 bytes
コンパイル時間 178 ms
コンパイル使用メモリ 81,964 KB
実行使用メモリ 65,280 KB
最終ジャッジ日時 2024-07-19 01:03:36
合計ジャッジ時間 1,950 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
62,592 KB
testcase_01 AC 48 ms
62,336 KB
testcase_02 AC 52 ms
65,280 KB
testcase_03 AC 51 ms
65,152 KB
testcase_04 AC 51 ms
65,080 KB
testcase_05 AC 52 ms
64,896 KB
testcase_06 AC 53 ms
65,280 KB
testcase_07 AC 53 ms
64,896 KB
testcase_08 AC 56 ms
65,152 KB
testcase_09 AC 47 ms
62,720 KB
testcase_10 AC 48 ms
62,720 KB
testcase_11 AC 50 ms
62,464 KB
testcase_12 AC 47 ms
62,684 KB
testcase_13 AC 48 ms
62,600 KB
testcase_14 AC 52 ms
65,152 KB
testcase_15 AC 52 ms
65,024 KB
testcase_16 AC 52 ms
64,768 KB
testcase_17 AC 53 ms
64,768 KB
testcase_18 AC 52 ms
65,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

p, g, ig = p, g, ig = 100000000000006291457, 3, 33333333333335430486
W = [pow(g, (p - 1) >> i, p) for i in range(24)]
iW = [pow(ig, (p - 1) >> i, p) for i in range(24)]
mod = p

def convolve(a, b):
    def fft(f):
        for l in range(k, 0, -1):
            d = 1 << l - 1
            U = [1]
            for i in range(d):
                U.append(U[-1] * W[l] % p)

            for i in range(1 << k - l):
                for j in range(d):
                    s = i * 2 * d + j
                    t = s + d
                    f[s], f[t] = (f[s] + f[t]) % p, U[j] * (f[s] - f[t]) % p

    def ifft(f):
        for l in range(1, k + 1):
            d = 1 << l - 1
            U = [1]
            for i in range(d):
                U.append(U[-1] * iW[l] % p)

            for i in range(1 << k - l):
                for j in range(d):
                    s = i * 2 * d + j
                    t = s + d
                    f[s], f[t] = (f[s] + f[t] * U[j]) % p, (f[s] - f[t] * U[j]) % p

    n0 = len(a) + len(b) - 1
    if len(a) < 50 or len(b) < 50:
        ret = [0] * n0
        if len(a) > len(b): a, b = b, a
        for i, aa in enumerate(a):
            for j, bb in enumerate(b):
                ret[i+j] = (ret[i+j] + aa * bb) % p
        return ret
    
    k = (n0).bit_length()
    n = 1 << k
    a = a + [0] * (n - len(a))
    b = b + [0] * (n - len(b))
    fft(a), fft(b)
    for i in range(n):
        a[i] = a[i] * b[i] % p
    ifft(a)
    invn = pow(n, p - 2, p)
    for i in range(n0):
        a[i] = a[i] * invn % p
    del a[n0:]
    return a

def find_generating_function(A):
    N = len(A)
    B = [1]
    C = [1]
    l, m, b = 0, 1, 1
    for i in range(N):
        d = A[i]
        for j in range(1, l + 1):
            d = (d + C[j] * A[i-j]) % mod
        if d == 0:
            m += 1
            continue
        T = C[:]
        ibd = pow(b, mod - 2, mod) * d % mod
        C += [0] * (len(B) + m - len(C))
        for j in range(len(B)):
            C[j + m] = (C[j + m] - ibd * B[j]) % mod
        if l * 2 <= i:
            B = T
            l, m, b = i + 1 - l, 1, d
        else:
            m += 1
    g = C
    f = convolve(A[:len(g)], g)[:len(g) - 1]    
    return f, g

def coef_of_generating_function(f, g, n):
    assert g[0] == 1 and len(g) == len(f) + 1
 
    while n:
        gg = [mod - a if i & 1 else a for i, a in enumerate(g)]
        f = convolve(f, gg)[n&1::2]
        g = convolve(g, gg)[::2]
        n >>= 1
    return f[0]

X = [{(0, 0)}]
dd = [(1, 1), (0, 1), (-1, 1), (-1, -1), (1, -1)]
for _ in range(10):
    pr = X[-1]
    ne = set(pr)
    for x, y in pr:
        for dx, dy in dd:
            nx, ny = x + dx, y + dy
            ne.add((nx, ny))
    X.append(ne)
    # print(len(ne))
A = [len(x) for x in X]
# print("A =", A)

f, g = find_generating_function(A)

n = int(input())
print(coef_of_generating_function(f, g, n))

0