結果
問題 | No.217 魔方陣を作ろう |
ユーザー |
![]() |
提出日時 | 2015-08-13 01:32:08 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2 ms / 5,000 ms |
コード長 | 2,788 bytes |
コンパイル時間 | 723 ms |
コンパイル使用メモリ | 88,248 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-18 09:09:44 |
合計ジャッジ時間 | 1,798 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 18 |
ソースコード
#define _USE_MATH_DEFINES#include <iostream>#include <iomanip>#include <sstream>#include <algorithm>#include <cmath>#include <string>//#include <array>#include <list>#include <queue>#include <vector>#include <complex>#include <set>#include <map>/////////#define REP(i, x, n) for(int i = x; i < n; i++)#define rep(i,n) REP(i,0,n)#define P(p) cout<<(p)<<endl;#define PII pair<int,int>/////////typedef long long LL;typedef long double LD;/////////using namespace::std;/////////void ki(int* d,int N){int temp,sa,sb;sa = 0;sb = 0;rep(i,N*N){temp = (N+(N-i)%N+sa)%N * N + (i+N/2+(N+(-sb)%N)%N)%N;if( d[temp] == 0){d[temp] = i+1;}else{sa += 2;++sb ;temp = (N+(N-i)%N+sa)%N * N + (i+N/2+(N+(-sb)%N)%N)%N;d[temp] = i+1;}}}void view(int* d,int N){rep(i,N*N){cout << d[i];if( i % N == N-1){cout << '\n';}else{cout << ' ';}}}int main(void){std::cin.tie(0);std::ios::sync_with_stdio(false);std::cout << std::fixed;////cout << setprecision(16);//const int dmax = 400;int d[dmax];int N;cin>>N;fill_n( d,N*N, 0);if( N%2 == 1){ki(d,N);}else if( N%4 == 0){rep(i,N)rep(j,N){if( (i%4)==(j%4) || 3-(i%4)==(j%4) ){d[i*N+j] = j+N*i+1;}}for(int i=0;i<N*N;++i){if( d[N*N-1-i] == 0){d[N*N-1-i] = i+1;}}}else{int temp;int dd[dmax];int n = N/2;int m = (N-2)/4;fill_n( dd,N*N, 0);ki(dd,n);rep(i,n)rep(j,n){d[i*N+j] = dd[i*n+j];//Ad[i*N+j+n] = dd[i*n+j] +2*n*n;//Bd[(i+n)*N +j] = dd[i*n+j] +3*n*n;//Cd[(i+n)*N +j+n] = dd[i*n+j] +n*n;//D}//縦ラインはそろっている。//view(d,N);cout << endl;rep(i,n){//A<->Cif(i==n/2){for(int j=1;j<=m;++j){temp = d[(i+n)*N +j];d[(i+n)*N +j] = d[i*N+j];d[i*N+j] = temp;}}else{for(int j=0;j<m;++j){temp = d[(i+n)*N+j];d[(i+n)*N+j] = d[i*N+j];d[i*N+j] = temp;}}}//view(d,N);cout << endl;//B<->Dfor(int j= m+2; j<2*m+1; ++j){rep(i,n){temp = d[(i+n)*N+j+n];d[(i+n)*N +j+n] = d[i*N+j+n];d[i*N+j+n] = temp;}}}//表示rep(i,N*N){cout << d[i];if( i % N == N-1){cout << '\n';}else{cout << ' ';}}//checkint sum = (1+N*N)*N*N/2/N;int ter;rep(i,N){ter = 0;rep(j,N){ter += d[i*N+j];}if( sum != ter){cout << i << "- " << ter << endl;}}rep(i,N){ter = 0;rep(j,N){ter += d[j*N + i];}if( sum != ter){cout << i << "| " << ter << endl;}}ter = 0;rep(i,N){ter += d[i*N+i];}if( sum != ter ){cout << "\" << ter << endl;}ter = 0;rep(i,N){ter += d[(N-1-i)*N+i];}if( sum != ter){cout << "/" << ter << endl;}return 0;}