답안 #946225

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
946225 2024-03-14T12:26:03 Z NourWael 아름다운 순열 (IZhO12_beauty) C++17
100 / 100
797 ms 164820 KB
#include <bits/extc++.h>
#define int long long 
using namespace std; 
using namespace __gnu_pbds; 
int dp[(1<<20)][20];
int two[20], thre[20];

signed main() {
   ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
   
   int n; cin>>n;
   int a[20], ind = 0;
   for(int i=0; i<n; i++) {
      cin>>a[i];
      int cnt = 0, x = a[i];
      while(x) {
         cnt += (x&1);
         x = x>>1;
      }
      two[i] = cnt;
      x = a[i], cnt = 0;
      int b = 387420489;
      while(x) {
         if(b*2<=x) { x-=b*2; }
         else if(b<=x) { x-=b; cnt++; }
         b/=3;
      }
      thre[i] = cnt;
   }
   
   for(int i=0; i<n; i++)  dp[(1<<i)][i] = 1;
   
   for(int mask = 1;  mask<(1<<n); mask++) {

      for(int i=0; i<n; i++) {
         if(mask&(1<<i)) continue;
         for(int j=0; j<20; j++) {
            if(mask&(1<<j) && (two[j]==two[i] || thre[j]==thre[i])) dp[mask+(1<<i)][i] += dp[mask][j];
         }
      } 

   }

   int fin = 0;
   for(int i=0; i<20; i++) fin += dp[(1<<n)-1][i];
   cout<<fin;
   return 0;
}

Compilation message

beauty.cpp: In function 'int main()':
beauty.cpp:12:15: warning: unused variable 'ind' [-Wunused-variable]
   12 |    int a[20], ind = 0;
      |               ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 500 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 852 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 8 ms 4740 KB Output is correct
12 Correct 7 ms 4560 KB Output is correct
13 Correct 31 ms 10844 KB Output is correct
14 Correct 161 ms 41808 KB Output is correct
15 Correct 150 ms 41556 KB Output is correct
16 Correct 162 ms 41548 KB Output is correct
17 Correct 169 ms 41556 KB Output is correct
18 Correct 185 ms 41608 KB Output is correct
19 Correct 797 ms 164820 KB Output is correct
20 Correct 665 ms 164756 KB Output is correct