/*
This code belongs to Aadit Ambadkar
Date: 2022-02-02 21:55:57
Problem: br
*/
#include <bits/stdc++.h>
using namespace::std;
typedef long long ll;
#define F0R(i, n) for (int i = 0; i < n; i++)
#define R0F(i, n) for (int i = n-1; i >= 0; i--)
#define FOR(i, a, n) for (int i = a; i < n; i++)
#define pb push_back
#define fastio ios::sync_with_stdio(0); cin.tie(0)
#define MOD 1000000007
#define FF first
#define SS second
int v[21];
int cnt2[21],cnt3[21];
ll dp[21][(1<<21)+10];
int count3(int x){
int resp = 0;
while(x){
if(x%3==1)resp++;
x/=3;
}
return resp;
}
int n;
int can[21][21];
bool mark[21][1<<22];
ll solve(int cur,int mask){
if(mark[cur][mask])return dp[cur][mask];
mark[cur][mask] = 1;
if(mask==(1<<n)-1)return dp[cur][mask] = 1LL;
for(int i=0;i<n;i++){
if((mask&(1<<i))==0&&can[cur][i])dp[cur][mask]+=solve(i,mask|(1<<i));
}
return dp[cur][mask];
}
int main() {
fastio;
cin >> n;
F0R(i, n) {
cin >> v[i];
cnt2[i] = __builtin_popcount(v[i]);
cnt3[i] = count3(v[i]);
}
F0R(i, n) F0R(j, n) if(i!=j&&(cnt2[i]==cnt2[j]||cnt3[i]==cnt3[j]))can[i][j] = 1;
ll ans = 0;
F0R(i, n) ans += solve(i,1<<i);
cout<<ans<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
316 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
436 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
10 ms |
2292 KB |
Output is correct |
12 |
Correct |
7 ms |
2124 KB |
Output is correct |
13 |
Correct |
56 ms |
8352 KB |
Output is correct |
14 |
Correct |
313 ms |
34364 KB |
Output is correct |
15 |
Correct |
435 ms |
34628 KB |
Output is correct |
16 |
Correct |
258 ms |
34244 KB |
Output is correct |
17 |
Correct |
368 ms |
34504 KB |
Output is correct |
18 |
Correct |
216 ms |
34180 KB |
Output is correct |
19 |
Correct |
2162 ms |
144952 KB |
Output is correct |
20 |
Correct |
2700 ms |
145444 KB |
Output is correct |