#include<bits/stdc++.h>
using namespace std;
long long n,arr[21],bi[21],ti[21];
long long dp[(1<<20)][20];
long long solve(int mask,int la){
if(mask==(1<<n)-1)return 1;
if(dp[mask][la]!=-1)return dp[mask][la];
long long c1 = 0;
for(int i = 0;i<n;i++){
if(mask&(1<<i))continue;
if(mask==0||bi[i]==bi[la]||ti[i]==ti[la]){
c1+=solve(mask|(1<<i),i);
}
}
return dp[mask][la] = c1;
}
int main(){
cin>>n;
for(int i = 0;i<n;i++){
cin>>arr[i];
for(int j = 29;j>=0;j--){
if(arr[i]&(1<<j)){
bi[i]++;
}
}
long long tmp = arr[i];
long long ans = 1;
for(int j = 1;j<=19;j++)ans*=3;
for(long long j = 19;j>=0;j--){
int z = 0;
while(tmp>=ans){
tmp-=ans;z++;
}
ans/=3;
if(z==1){
ti[i]++;
}
}
}
memset(dp,-1,sizeof dp);
cout<<solve(0,0)<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
164400 KB |
Output is correct |
2 |
Correct |
47 ms |
164320 KB |
Output is correct |
3 |
Correct |
47 ms |
164412 KB |
Output is correct |
4 |
Correct |
50 ms |
164416 KB |
Output is correct |
5 |
Correct |
45 ms |
164392 KB |
Output is correct |
6 |
Correct |
47 ms |
164324 KB |
Output is correct |
7 |
Correct |
48 ms |
164328 KB |
Output is correct |
8 |
Correct |
47 ms |
164444 KB |
Output is correct |
9 |
Correct |
47 ms |
164316 KB |
Output is correct |
10 |
Correct |
47 ms |
164428 KB |
Output is correct |
11 |
Correct |
58 ms |
164540 KB |
Output is correct |
12 |
Correct |
53 ms |
164388 KB |
Output is correct |
13 |
Correct |
93 ms |
164372 KB |
Output is correct |
14 |
Correct |
314 ms |
164440 KB |
Output is correct |
15 |
Correct |
359 ms |
164484 KB |
Output is correct |
16 |
Correct |
279 ms |
164440 KB |
Output is correct |
17 |
Correct |
328 ms |
164444 KB |
Output is correct |
18 |
Correct |
227 ms |
164336 KB |
Output is correct |
19 |
Correct |
1981 ms |
164440 KB |
Output is correct |
20 |
Correct |
2443 ms |
164436 KB |
Output is correct |