結果

問題 No.217 魔方陣を作ろう
ユーザー koba-e964koba-e964
提出日時 2016-12-18 00:53:40
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,222 bytes
コンパイル時間 825 ms
コンパイル使用メモリ 94,600 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-21 00:17:04
合計ジャッジ時間 2,317 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;
const ll mod = 1e9 + 7;

vector<VI> odd(int n) {
  vector<VI> tbl(n, VI(n, -100));
  REP(i, 0, n) {
    int x = (2 * i) % n;
    int y = (n / 2 - i + n) %  n;
    REP(j, 0, n) {
      tbl[(x - j + n) % n][(y + j) % n] = 1 + n * i + j;
    }
  }
  return tbl;
}

vector<VI> multiple4(int n) {
  vector<VI> tbl(n, VI(n, -100));
  REP(i, 0, n) {
    REP(j, 0, n) {
      int a = i & 3;
      int b = j & 3;
      if (0x9669 & 1 << (4 * a + b)) { // on a diagonal
	tbl[i][j] = 1 + i * n + j;
      } else {
	tbl[n - 1 - i][n - 1 - j] = 1 + i * n + j;
      }
    }
  }
  return tbl;
      
}

vector<VI> rest(int n) {
  vector<VI> sub = odd(n / 2);
  int l[2][2] = {
    {0, -3},
    {-2, -1}};
  int u[2][2] = {
    {-3, 0},
    {-2, -1}};
  int x[2][2] = {
    {-3, 0},
    {-1, -2}};
  int(* mat[3])[2] = {l, u, x};
  vector<vector<int> > pow(n / 2, VI(n / 2));
  int m = n / 4; // n / 2 = 2m + 1
  REP(i, 0, m + 1) {
    REP(j, 0, n / 2) {
      pow[i][j] = 0;
    }
  }
  REP(j, 0, n / 2) {
    pow[m + 1][j] = 1;
  }
  REP(i, m + 2, n / 2) {
    REP(j, 0, n / 2) {
      pow[i][j] = 2;
    }
  }
  swap(pow[m][m], pow[m + 1][m]);
  vector<VI> tbl(n, VI(n));
  REP(i, 0, n / 2) {
    REP(j, 0, n / 2) {
      REP(k, 0, 2) {
	REP(p, 0, 2) {
	  tbl[2 * i + k][2 * j + p] = 4 * sub[i][j] + mat[pow[i][j]][k][p];
	}
      }
    }
  }
  return tbl;
}

int main(void){
  int n;
  cin >> n;
  vector<VI> tbl;
  if (n % 2 == 1) {
    tbl = odd(n);
  } else if (n % 4 == 0) {
    tbl = multiple4(n);
  } else {
    tbl = rest(n);
  }
  REP(i, 0, n) {
    REP(j, 0, n) {
      cout << tbl[i][j] << (j == n - 1 ? "\n" : " ");
    }
  }
}
0