Submission #74103

# Submission time Handle Problem Language Result Execution time Memory
74103 2018-08-30T05:44:51 Z charlies_moo Prosjecni (COCI16_prosjecni) C++
24 / 120
1000 ms 2324 KB
#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 1082 ms 376 KB Time limit exceeded
2 Correct 2 ms 476 KB Output is correct
3 Execution timed out 1084 ms 492 KB Time limit exceeded
4 Correct 2 ms 492 KB Output is correct
5 Execution timed out 1080 ms 492 KB Time limit exceeded
6 Execution timed out 1087 ms 584 KB Time limit exceeded
7 Execution timed out 1083 ms 1096 KB Time limit exceeded
8 Execution timed out 1078 ms 2052 KB Time limit exceeded
9 Execution timed out 1084 ms 2268 KB Time limit exceeded
10 Execution timed out 1080 ms 2324 KB Time limit exceeded