結果

問題 No.1491 銀将
ユーザー Kiri8128Kiri8128
提出日時 2021-04-30 21:46:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 88 ms / 1,000 ms
コード長 2,891 bytes
コンパイル時間 305 ms
コンパイル使用メモリ 87,140 KB
実行使用メモリ 76,528 KB
最終ジャッジ日時 2023-09-26 05:42:06
合計ジャッジ時間 3,347 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
76,104 KB
testcase_01 AC 79 ms
76,128 KB
testcase_02 AC 83 ms
76,528 KB
testcase_03 AC 88 ms
76,280 KB
testcase_04 AC 86 ms
76,120 KB
testcase_05 AC 87 ms
76,324 KB
testcase_06 AC 85 ms
76,336 KB
testcase_07 AC 84 ms
76,328 KB
testcase_08 AC 85 ms
76,336 KB
testcase_09 AC 81 ms
76,144 KB
testcase_10 AC 81 ms
76,040 KB
testcase_11 AC 81 ms
76,228 KB
testcase_12 AC 81 ms
76,104 KB
testcase_13 AC 81 ms
76,228 KB
testcase_14 AC 84 ms
76,384 KB
testcase_15 AC 88 ms
76,380 KB
testcase_16 AC 85 ms
76,492 KB
testcase_17 AC 88 ms
76,288 KB
testcase_18 AC 87 ms
76,436 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