Submission #90116

# Submission time Handle Problem Language Result Execution time Memory
90116 2018-12-20T11:15:31 Z YottaByte Beautiful row (IZhO12_beauty) C++14
0 / 100
3000 ms 684 KB
#include <iostream>
#include <vector>
using namespace std;
#define pb push_back
const int N = 20;
vector < int > a;
int n, ans;

inline bool count(int a, int b)
{
	int resa, resb;
	resa = resb = 0;
	
	while(a)
	{
		resa += (a % 3 == 1);
		a /= 3;
	}
	
	while(b)
	{
		resb += (b % 3 == 1);
		b /= 3;
	}
	return (resa == resb);
}

inline bool check(vector < int > b)
{
	for(int i = 0; i < n - 1; i++)
	{
		if(__builtin_popcount(b[i]) == __builtin_popcount(b[i + 1]));
		else if(count(b[i], b[i + 1]));
		else return false;
	}
	return true;
}

void bt(vector < int > b, vector < int > u)
{
	if(b.size() == n)
	{
		if(check(b)) ans++;
		return;
	}
	
	for(int i = 0; i < n; i++)
	{
		if(!u[i])
		{
			b.pb(a[i]);
			u[i] = 1;
			bt(b, u);
			b.pop_back();
			u[i] = 0;
		}
	}
}

void out()
{
	for(int i = 1; i <= n; i++)
	{
		cout << a[i] << " ";
	}
}

main()
{
	cin >> n;
	for(int i = 0; i < n; i++)
	{
		int x; cin >> x;
		a.pb(x);
	}
	
	vector < int > u, e;
	for(int i = 0; i < n; i++)
		u.pb(0);
	
	e.clear();
	bt( e, u );
	
	cout << ans << endl;
}

Compilation message

beauty.cpp: In function 'void bt(std::vector<int>, std::vector<int>)':
beauty.cpp:41:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  if(b.size() == n)
     ~~~~~~~~~^~~~
beauty.cpp: At global scope:
beauty.cpp:68:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main()
      ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 376 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 588 KB Output is correct
4 Correct 2 ms 588 KB Output is correct
5 Correct 2 ms 588 KB Output is correct
6 Correct 1735 ms 588 KB Output is correct
7 Correct 1686 ms 588 KB Output is correct
8 Correct 1763 ms 684 KB Output is correct
9 Correct 1689 ms 684 KB Output is correct
10 Correct 2148 ms 684 KB Output is correct
11 Execution timed out 3035 ms 684 KB Time limit exceeded
12 Halted 0 ms 0 KB -