結果

問題 No.3676 Cuboid Alignment
コンテスト
ユーザー 👑 みうね
提出日時 2026-08-10 17:39:35
言語 Python3
(3.14.7 + numpy 2.5.2 + scipy 1.18.0)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 1,760 ms / 2,000 ms
+ 222µs
コード長 1,943 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 568 ms
コンパイル使用メモリ 21,336 KB
実行使用メモリ 143,384 KB
最終ジャッジ日時 2026-09-04 22:19:34
合計ジャッジ時間 7,886 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#!/usr/bin/env python3

import sys

import numpy as np
from scipy.fft import fftn, ifftn, next_fast_len


BLACK = ord('B')
WHITE = ord('W')


def solve() -> None:
    tokens = sys.stdin.buffer.read().split()
    if not tokens:
        return

    x_size, y_size, z_size = map(int, tokens[:3])
    cells = np.frombuffer(b''.join(tokens[3:]), dtype=np.uint8).reshape(
        2, z_size, y_size, x_size
    )
    first, second = cells

    # The real part of
    #   (A_black + i A_white) * (B_white - i B_black)
    # is A_black * B_white + A_white * B_black.
    first_packed = (first == BLACK).astype(np.complex128)
    first_packed.imag = first == WHITE

    reversed_second = second[::-1, ::-1, ::-1]
    second_packed = (reversed_second == WHITE).astype(np.complex128)
    second_packed.imag = -(reversed_second == BLACK).astype(np.float64)

    convolution_shape = (
        2 * z_size - 1,
        2 * y_size - 1,
        2 * x_size - 1,
    )
    transform_shape = tuple(next_fast_len(length) for length in convolution_shape)

    transformed = fftn(
        first_packed, s=transform_shape, workers=-1, overwrite_x=True
    )
    transformed *= fftn(
        second_packed, s=transform_shape, workers=-1, overwrite_x=True
    )
    convolution = np.rint(
        ifftn(transformed, workers=-1, overwrite_x=True).real[
            :convolution_shape[0],
            :convolution_shape[1],
            :convolution_shape[2],
        ]
    ).astype(np.int64)

    # Fold every linear-convolution coordinate modulo its original axis.
    convolution[:, :, :x_size - 1] += convolution[:, :, x_size:]
    convolution = convolution[:, :, :x_size]
    convolution[:, :y_size - 1, :] += convolution[:, y_size:, :]
    convolution = convolution[:, :y_size, :]
    convolution[:z_size - 1, :, :] += convolution[z_size:, :, :]
    convolution = convolution[:z_size, :, :]

    print(int(convolution.min()))


if __name__ == '__main__':
    solve()
0