#include <cstdio>
#include <iostream>
using namespace std;
long long n;
int matrix[101][101];
bool used[100000001];
bool check()
{
long long row[100],col[100];
for(long long i=0;i<n;i++)
{
int count_=0;
row[i]=0;
for(long long j=0;j<n;j++)
row[i]+=matrix[i][j];
for(long long j=0;j<n;j++)
if(matrix[i][j]*n!=row[i])
count_++;
if(count_==n)
return false;
}
for(long long j=0;j<n;j++)
{
int count_=0;
col[j]=0;
for(long long i=0;i<n;i++)
col[j]+=matrix[i][j];
if(count_==n)
return false;
}
return true;
}
bool flag=false;
void vio(int x,int y)
{
if(flag)
return;
if(x==n && y==n)
{
if(check())
{
flag=true;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
cout<<matrix[i][j]<<' ';
cout<<endl;
}
}
return;
}
if(y==n)
x++,y=0;
for(int i=1;i<=n*n*n;i++)
{
if(flag)
return;
if(used[i])
continue;
used[i]=1;
matrix[x][y]=i;
vio(x,y+1);
if(flag)
return;
used[i]=0;
}
}
int main()
{
cin>>n;
vio(0,0);
if(!flag)
cout<<"-1";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1079 ms |
252 KB |
Time limit exceeded |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Execution timed out |
1065 ms |
416 KB |
Time limit exceeded |
4 |
Correct |
2 ms |
420 KB |
Output is correct |
5 |
Execution timed out |
1075 ms |
556 KB |
Time limit exceeded |
6 |
Execution timed out |
1085 ms |
784 KB |
Time limit exceeded |
7 |
Execution timed out |
1077 ms |
1188 KB |
Time limit exceeded |
8 |
Execution timed out |
1089 ms |
2132 KB |
Time limit exceeded |
9 |
Execution timed out |
1078 ms |
2256 KB |
Time limit exceeded |
10 |
Execution timed out |
1065 ms |
2388 KB |
Time limit exceeded |