# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
716435 | vjudge1 | Beautiful row (IZhO12_beauty) | C++17 | 2531 ms | 164556 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5e5 + 100;
const int mod = (int)1e9+7;
int n;
int a[maxn];
int g[22][22];
ll dp[1<<20][20];
int ter(int x) {
int cnt = 0;
while(x > 0) {
if(x % 3 == 1) cnt++;
x /= 3;
}
return cnt;
}
ll rec(int mask, int last) {
if(__builtin_popcount(mask) == n) {
return 1;
}
if(dp[mask][last] != -1) return dp[mask][last];
dp[mask][last] = 0;
for(int i = 0; i < n; i++) {
if(g[last][i] && !(mask & (1<<i))) {
dp[mask][last] += rec(mask|1<<i, i);
}
}
return dp[mask][last];
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n;
for(int i = 0; i < n; i++) {
cin >> a[i];
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(__builtin_popcount(a[i]) == __builtin_popcount(a[j])) {
g[i][j] = 1;
} else {
if(ter(a[i]) == ter(a[j])) {
g[i][j] = 1;
}
}
}
}
memset(dp, -1, sizeof dp);
ll ans = 0;
for(int i = 0; i < n; i++) {
ans += rec(1<<i, i);
}
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |