Submission #339841

# Submission time Handle Problem Language Result Execution time Memory
339841 2020-12-26T09:42:00 Z nandonathaniel Beautiful row (IZhO12_beauty) C++14
0 / 100
3000 ms 164668 KB
#include<bits/stdc++.h>
using namespace std;
const int MAXN=20;

int n;
long long dp[1<<MAXN][MAXN];
int a[MAXN];

int binary(int x){
	return __builtin_popcount(x);
}

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

int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin >> n;
	for(int i=0;i<n;i++){
		cin >> a[i];
		dp[1<<i][i]=1;
	}
	for(int i=1;i<(1<<n);i++){
		for(int j=0;j<n;j++){
			if((1<<j) & i){
				for(int k=0;k<n;k++){
					if(!((1<<k) & i)){
						if(binary(a[k])==binary(a[j]) || ternary(a[k])==ternary(a[j]))dp[i+(1<<k)][k]+=dp[i][j];
					}
				}
			}
		}
	}
	long long ans=0;
	for(int i=0;i<n;i++)ans+=dp[(1<<n)-1][i];
	cout << ans << '\n';
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 2 ms 620 KB Output is correct
10 Correct 2 ms 492 KB Output is correct
11 Correct 20 ms 2924 KB Output is correct
12 Correct 24 ms 3052 KB Output is correct
13 Correct 94 ms 10732 KB Output is correct
14 Correct 568 ms 41580 KB Output is correct
15 Correct 308 ms 41452 KB Output is correct
16 Correct 698 ms 41452 KB Output is correct
17 Correct 639 ms 41360 KB Output is correct
18 Correct 831 ms 41436 KB Output is correct
19 Execution timed out 3061 ms 164668 KB Time limit exceeded
20 Halted 0 ms 0 KB -