# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
106206 | Hideo | Beautiful row (IZhO12_beauty) | C++14 | 16 ms | 2944 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;
#define ll long long
#define pb push_back
#define mk make_pair
#define fr first
#define sc second
#define vi vector < int >
#define vl vector < ll >
#define pi pair < int, int >
#define pii pair < int, pi >
#define vii vector < pi >
const int N = 1e5 + 7;
const int mod = 1e9 + 7;
int b[20], t[20];
int n;
ll dp[(1 << 20)][20];
ll ans;
int bin (int x, int c = 0){
if (x % 2 == 1)
c++;
if (!x)
return c;
bin(x / 2, c);
}
int ter (int x, int c = 0){
if (x % 3 == 1)
c++;
if (!x)
return c;
ter(x / 3, c);
}
main(){
cin >> n;
for (int i = 0; i < n; i++){
int a;
cin >> a;
b[i] = bin(a);
t[i] = ter(a);
}
for (int i = 0; i < n; i++)
dp[(1 << i)][i] = 1;
for (int val = 2; val <= n; val++){
for (int mask = 1; mask < (1 << n); mask++){
int cnt = 0;
for (int sub = 0; sub < n; sub++)
if (mask & (1 << sub))
cnt++;
if (cnt != val)
continue;
for (int sub = 0; sub < n; sub++){
if (mask & (1 << sub)){
int submask = (mask ^ (1 << sub));
for (int last = 0; last < n; last++)
if (last != sub && submask & (1 << last) && (b[last] == b[sub] || t[last] == t[sub]))
dp[mask][sub] += dp[submask][last];
dp[mask][sub] %= mod;
}
}
}
}
int mask = 0;
for (int i = 0; i < n; i++)
mask += (1 << i);
for (int last = 0; last < n; last++)
ans += dp[mask][last];
cout << ans % mod;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |