結果

問題 No.217 魔方陣を作ろう
ユーザー maspymaspy
提出日時 2020-03-04 16:57:31
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 612 ms / 5,000 ms
コード長 1,650 bytes
コンパイル時間 150 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 44,368 KB
最終ジャッジ日時 2024-04-22 01:43:04
合計ジャッジ時間 11,750 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 550 ms
43,988 KB
testcase_01 AC 542 ms
43,864 KB
testcase_02 AC 547 ms
43,604 KB
testcase_03 AC 546 ms
44,112 KB
testcase_04 AC 539 ms
43,600 KB
testcase_05 AC 546 ms
44,112 KB
testcase_06 AC 544 ms
43,612 KB
testcase_07 AC 542 ms
44,116 KB
testcase_08 AC 540 ms
43,764 KB
testcase_09 AC 542 ms
43,864 KB
testcase_10 AC 541 ms
43,868 KB
testcase_11 AC 540 ms
43,740 KB
testcase_12 AC 541 ms
44,368 KB
testcase_13 AC 538 ms
44,116 KB
testcase_14 AC 545 ms
44,196 KB
testcase_15 AC 537 ms
43,736 KB
testcase_16 AC 546 ms
43,848 KB
testcase_17 AC 612 ms
43,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
# %%
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


# %%
import numpy as np

N = int(read())

# %%


def f(N):
    if N % 4 == 0:
        return f0(N)
    elif N % 4 == 2:
        return f2(N)
    else:
        return f13(N)


def f13(N):
    A = np.zeros((N, N), np.int32)
    x = 0
    y = N // 2
    for i in range(1, N * N + 1):
        if A[x][y]:
            x += 2
            y -= 1
            x %= N
            y %= N
        A[x, y] = i
        x -= 1
        y += 1
        x %= N
        y %= N
    return A


def f0(N):
    A = np.arange(N * N).reshape(N, N)
    
    A[1::4, 0::4] *= -1
    A[1::4, 3::4] *= -1
    A[2::4, 0::4] *= -1
    A[2::4, 3::4] *= -1
    A[0::4, 1::4] *= -1
    A[0::4, 2::4] *= -1
    A[3::4, 1::4] *= -1
    A[3::4, 2::4] *= - 1
    A[A < 0] += N * N - 1
    A += 1
    return A


def f2(N):
    A = f(N // 2)
    A = np.repeat(A, 2, axis=0).repeat(2, axis=1)
    A = (A - 1) * 4
    n = N // 4
    # L-type
    A[:2 * n + 2:2, ::2] += 4
    A[:2 * n + 2:2, 1::2] += 1
    A[1:2 * n + 2:2, ::2] += 2
    A[1: 2 * n + 2:2, 1::2] += 3
    # U-type
    A[2 * n + 2, ::2] += 1
    A[2 * n + 2, 1::2] += 4
    A[2 * n + 3, ::2] += 2
    A[2 * n + 3, 1::2] += 3
    # X-type
    A[2 * n + 4::2, ::2] += 1
    A[2 * n + 4::2, 1::2] += 4
    A[2 * n + 5::2, ::2] += 3
    A[2 * n + 5::2, 1::2] += 2
    # modify center
    A[2 * n, 2 * n] -= 3
    A[2 * n, 2 * n + 1] += 3
    A[2 * n + 2, 2 * n] += 3
    A[2 * n + 2, 2 * n + 1] -= 3
    return A


# %%
A = f(N)
print('\n'.join(' '.join(row) for row in A.astype(str)))
0