#include<bits/stdc++.h>
#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define dec(x, y) fixed << setprecision((y)) << (x)
#define xx first
#define yy second
#define srt(v) sort((v).begin(), (v).end())
#define srtr(v) sort((v).rbegin(), (v).rend())
#define pb push_back
#define popb pop_back
#define sz(a) (int)(a).size()
#define len(a) (int)(a).length()
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
int n, a[22];
ll dp[1100000][22];
bool mogu(int pr, int dr) {
pr=a[pr]; dr=a[dr];
if(__builtin_popcount(pr)==__builtin_popcount(dr)) return true;
int tn1=0, tn2=0;
int m1=pr, m2=dr;
while(m1>0) {
int cif=m1%3;
m1/=3;
if(cif==1) ++tn1;
}
while(m2>0) {
int cif=m2%3;
m2/=3;
if(cif==1) ++tn2;
}
return (tn1==tn2);
}
int main() {
ios;
cin >> n;
for(int i=0; i<n; ++i) cin >> a[i];
for(int i=0; i<n; ++i) dp[0][i]=1;
for(int mask=1; mask<(1<<n); ++mask) {
for(int last=0; last<n; ++last) {
if(((1<<last)&mask)==0) continue;
if(__builtin_popcount(mask)==1) {
dp[mask][last]=1;
continue;
}
for(int j=0; j<n; ++j) {
if(j==last || ((1<<j)&mask)==0) continue;
if(mogu(j, last)) dp[mask][last]+=dp[mask^(1<<last)][j];
}
}
}
ll suma=0LL;
for(int i=0; i<n; ++i) suma+=dp[(1<<n)-1][i];
cout << suma;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Correct |
2 ms |
448 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
2 ms |
460 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
11 |
Correct |
21 ms |
3148 KB |
Output is correct |
12 |
Correct |
22 ms |
3004 KB |
Output is correct |
13 |
Correct |
105 ms |
11588 KB |
Output is correct |
14 |
Correct |
593 ms |
45324 KB |
Output is correct |
15 |
Correct |
363 ms |
45380 KB |
Output is correct |
16 |
Correct |
715 ms |
45496 KB |
Output is correct |
17 |
Correct |
675 ms |
45432 KB |
Output is correct |
18 |
Correct |
840 ms |
45440 KB |
Output is correct |
19 |
Execution timed out |
3106 ms |
178824 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |