# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
857555 | Mohamed_Kachef06 | 아름다운 순열 (IZhO12_beauty) | C++17 | 3046 ms | 164700 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define A first
#define B second
int n , a[20] , dp[20][1<<20] , co[20][20];
int fact(int i , int b){
int x = a[i];
int ans =0 ;
while(x > 0){
if (x%b == 1) ans++;
x/=b;
}
return ans;
}
void pre(){
for (int i = 0 ; i < n ; i++) {
for (int j = 0 ; j < i ; j++){
int a = fact(i ,2), b = fact(i , 3) , c = fact(j ,2 ) , d= fact(j , 3);
if (a == c || b == d) { co[i][j] = 1; co[j][i] = 1;}
}
}
}
int brute(int last , int mask){
if ( mask == (1<<n) - 1) return 1;
if (~dp[last][mask]) return dp[last][mask];
int ans = 0 ;
for (int i = 0 ; i < n ; i++){
if (!(mask & (1<<i))){
if (mask == 0 || co[last][i]) ans += brute(i , (mask | (1<<i)));
}
}
return dp[last][mask] = ans;
}
signed main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 0 ; i < n ; i++) cin >> a[i];
memset(dp , -1 , sizeof dp);
pre();
cout << brute(0 , 0);
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |