#include <bits/stdc++.h>
#define MAX 1100000
using ll = long long;
ll getval(ll x,ll t){
ll ans=0;
while(x){
ll p = x%t;
if(p==1)++ans;
x/=t;
}
return ans;
}
long long ind[21][2];
bool existe[MAX][20];
ll tab[MAX][20];
ll obj=0;
int N;
ll dp(ll pente,int pos){
if(pente==obj)return 1;
if(existe[pente][pos])return tab[pente][pos];
existe[pente][pos]=true;
ll tot=0;
for(int i=0;i!=N;++i){
if(pente&(1<<i))continue;
if(ind[pos][0]==ind[i][0]
||ind[pos][1]==ind[i][1]){
tot+=dp(pente+(1<<i),i);
}
}
return tab[pente][pos]=tot;
}
int main()
{
std::cin>>N;
obj=(1<<N)-1;
for(int i=0;i!=N;++i){
ll x;std::cin>>x;
ind[i][0]=getval(x,2);
ind[i][1]=getval(x,3);
}
ll ans=0;
for(int i=0;i!=N;++i){
ans+=dp(1<<i,i);
}
std::cout<<ans<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
432 KB |
Output is correct |
11 |
Correct |
12 ms |
3200 KB |
Output is correct |
12 |
Correct |
9 ms |
3156 KB |
Output is correct |
13 |
Correct |
54 ms |
11820 KB |
Output is correct |
14 |
Correct |
313 ms |
46468 KB |
Output is correct |
15 |
Correct |
415 ms |
46392 KB |
Output is correct |
16 |
Correct |
248 ms |
46464 KB |
Output is correct |
17 |
Correct |
397 ms |
46468 KB |
Output is correct |
18 |
Correct |
200 ms |
46468 KB |
Output is correct |
19 |
Correct |
2125 ms |
184976 KB |
Output is correct |
20 |
Correct |
2233 ms |
184908 KB |
Output is correct |