Submission #883629

# Submission time Handle Problem Language Result Execution time Memory
883629 2023-12-05T14:30:34 Z NintsiChkhaidze Beautiful row (IZhO12_beauty) C++17
100 / 100
618 ms 189324 KB
#include <bits/stdc++.h>
#define pb push_back
#define s second
#define f first
#define left (h<<1),l,(l + r)/2
#define right ((h<<1)|1),(l + r)/2 + 1,r
#define pii pair<int,int>
#define ll long long
#define int ll
using namespace std;

const int N = 1e5 + 3;

ll dp[(1<<20) + 3][23];
int p2[23],p3[23],a[23];

signed main (){
    ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
	
	int n;
	cin>>n;

	for (int i = 0; i < n; i++)
		cin >> a[i],dp[(1<<i)][i] = 1;

	for (int i = 0; i < n; i++){
		p2[i] = __builtin_popcount(a[i]);
		while (a[i] > 0){
			p3[i] += (a[i] % 3 == 1);
			a[i] /= 3;
		}
	}

	for (int mask = 0; mask < (1<<n); mask++){
		vector <int> on,off;
		for (int i=0;i<n;i++){
			if (!(mask & (1<<i))) off.pb(i);
			else on.pb(i);
		}

		for (int x: on){
			for (int y: off){
				if (p2[x] == p2[y] || p3[x] == p3[y]) 
					dp[(mask ^ (1<<y))][y] += dp[mask][x];
			}
		}
	}

	ll ans = 0;
	for (int i = 0; i < n; i++)
		ans += dp[(1<<n) - 1][i];
	cout<<ans;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 6 ms 4700 KB Output is correct
12 Correct 6 ms 4700 KB Output is correct
13 Correct 26 ms 12892 KB Output is correct
14 Correct 130 ms 47804 KB Output is correct
15 Correct 117 ms 47816 KB Output is correct
16 Correct 142 ms 47780 KB Output is correct
17 Correct 143 ms 47788 KB Output is correct
18 Correct 151 ms 47808 KB Output is correct
19 Correct 618 ms 189288 KB Output is correct
20 Correct 529 ms 189324 KB Output is correct