Submission #679205

#TimeUsernameProblemLanguageResultExecution timeMemory
679205pragmatistBeautiful row (IZhO12_beauty)C++17
100 / 100
836 ms164464 KiB
#include<bits/stdc++.h> #define sz(v) (int)v.size() #define ll long long #define pb push_back #define x first #define y second #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define nl "\n" using namespace std; using pii = pair<int, int>; const int N = (int)3e5 + 7; // make sure this is right const int M = (int)1e6 + 7; const int inf = (int)1e9 + 7; const ll INF = (ll)3e18 + 7; const ll MOD = (ll)998244353; // make sure this is right bool bit(int x, int i) { return x >> i & 1; } int sum(int x, int y) { x += y; if(x >= MOD) x -= MOD; return x; } pii dir[] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; int n, b[20], c[20]; ll dp[1 << 20][20]; void solve() { cin >> n; for(int i = 0; i < n; ++i) { int x; cin >> x; b[i] = __builtin_popcount(x); while(x) { c[i] += (x % 3 == 1); x /= 3; } } for(int i = 0; i < n; ++i) dp[1 << i][i] = 1; for(int mask = 1; mask < (1 << n); ++mask) { for(int i = 0; i < n; ++i) { if(bit(mask, i)) { for(int j = 0; j < n; ++j) { if(!bit(mask, j) && (c[i] == c[j] || b[i] == b[j])) { dp[mask | (1 << j)][j] += dp[mask][i]; } } } } } ll ans = 0; for(int i = 0; i < n; ++i) { ans += dp[(1 << n) - 1][i]; } cout << ans; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); int test = 1; //cin >> test; for(int i = 1; i <= test; ++i) { //cout << "Case #" << i << ": "; solve(); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...