Submission #163493

# Submission time Handle Problem Language Result Execution time Memory
163493 2019-11-13T14:26:28 Z tselmegkh Beautiful row (IZhO12_beauty) C++14
0 / 100
2 ms 376 KB
#include<bits/stdc++.h>
using namespace std;

vector<int> binary[33], trinary[33];
vector<int> a;
int n;
bool same[25], block[25];

int count(int x){
	int res = 0;
	while(x > 0){
		if(x % 3 == 1){
			res++;
		}
		x /= 3;
	}
	return res;
}

map<pair<int, pair<int, string>>, bool> vis;
map<pair<int, pair<int, string>>, int> dp;
int solve(int i, int mask, string cur){
	if(i == n){
		return 1;
	}
	if(vis[{i, {mask, cur}}])return dp[{i, {mask, cur}}];
	vis[{i, {mask, cur}}] = 1;
	int ans = 0;

	if(i == 0){
		for(int j = 0; j < n; j++){
			int curmask = mask | (1 << j);
			string tmp;
			tmp += j + '0';
			ans += solve(i + 1, curmask, tmp);
		}
	}else{
		int last = cur.back() - '0';
		int binaryones = __builtin_popcount(a[last]), trinaryones = count(a[last]);
		for(int j = 0; j < binary[binaryones].size(); j++){
			int k = binary[binaryones][j];
			if(mask & (1 << k))continue;
			int curmask = mask | (1 << k);
			string tmp = cur;
			tmp += k + '0';
			ans += solve(i + 1, curmask, tmp);
		}
		for(int j = 0; j < trinary[trinaryones].size(); j++){
			int k = trinary[trinaryones][j];
			if(mask & (1 << k))continue;
			string tmp = cur;
			tmp += k + '0';
			int curmask = mask | (1 << k);
			ans += solve(i + 1, curmask, tmp);
		}
	}
	return dp[{i, {mask, cur}}] = ans;
}
int main(){
	cin >> n;
	a.resize(n);

	for(int i = 0; i < n; i++){
		cin >> a[i];
		int cnt3 = count(a[i]), cnt2 = __builtin_popcount(a[i]);
		binary[cnt2].push_back(i);
		trinary[cnt3].push_back(i);
	}

	cout << solve(0, 0, "") << '\n';
}

Compilation message

beauty.cpp: In function 'int solve(int, int, std::__cxx11::string)':
beauty.cpp:40:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int j = 0; j < binary[binaryones].size(); j++){
                  ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
beauty.cpp:48:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int j = 0; j < trinary[trinaryones].size(); j++){
                  ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Incorrect 2 ms 376 KB Output isn't correct
5 Halted 0 ms 0 KB -