#include <bits/stdc++.h>
#define int long long
using namespace std;
int n;
vector<int> nums;
int cnt=0ll;
vector<vector<int> > dp;
vector<int>b2,b3;
int slv(int msk=0,int lstidx=-1){
if(msk==(1ll<<n)-1ll){
return 1ll;
}
int cnt=0;
if(lstidx!=-1&&dp[msk][lstidx]!=-1)
return dp[msk][lstidx];
for(int i=0;i<n;i++){
if(!(msk&(1<<i))){
if(lstidx==-1){
cnt+=slv(msk|(1ll<<i),i);
continue;
}
int f=(b2[i]==b2[lstidx])||(b3[i]==b3[lstidx]);
if(f){
cnt+=slv(msk|(1ll<<i),i);
}
}
}
if(lstidx!=-1)
dp[msk][lstidx]=cnt;
return cnt;
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>n;
nums=vector<int>(n);
for(int i=0;i<n;i++){
cin>>nums[i];
}
dp=vector<vector<int> >((1<<n),vector<int>(n,-1ll));
b2=b3=vector<int>(n);
for(int i=0;i<n;i++){
int cntb2=0;
int num1=nums[i];
while(num1){
cntb2+=num1%2ll;
num1/=2ll;
}
b2[i]=cntb2;
int cntb3=0;
num1=nums[i];
while(num1){
cntb3+=(num1%3ll==1ll);
num1/=3ll;
}
b3[i]=cntb3;
}
cout<<(long long)slv();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
13 ms |
2796 KB |
Output is correct |
12 |
Correct |
10 ms |
2796 KB |
Output is correct |
13 |
Correct |
90 ms |
11116 KB |
Output is correct |
14 |
Correct |
545 ms |
47468 KB |
Output is correct |
15 |
Correct |
654 ms |
47468 KB |
Output is correct |
16 |
Correct |
436 ms |
47468 KB |
Output is correct |
17 |
Correct |
591 ms |
47468 KB |
Output is correct |
18 |
Correct |
361 ms |
47468 KB |
Output is correct |
19 |
Execution timed out |
3102 ms |
205548 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |