[BOJ] 16936 나3곱2

Time Lapse :41min 11sec

16936.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <vector>
#include <algorithm>
#include <list>
#include <stdlib.h>
typedef unsigned long long ull;
using namespace std;
ull n, num;
vector<ull> vec;

void dfs(list<ull> l) {
if(l.size() == n) {
for(ull n : l)
cout<<n<<" ";
exit(0);
}
ull n = l.back();
if((!(n % 3)) && (n / 3 == *lower_bound(vec.begin(), vec.end(), n / 3))) {
l.push_back(n/3);
dfs(l);
l.pop_back();
}
if((n<<1) == *lower_bound(vec.begin(), vec.end(), n<<1)) {
l.push_back(n<<1);
dfs(l);
l.pop_back();
}
}
int main(void) {
scanf("%d",&n);
vec.reserve(n + 1);
for(int i = 0; i < n; i++) {
cin>>num;
vec.push_back(num);
}
sort(vec.begin(), vec.end());
for(int i = 0; i < n; ++i) {
dfs(list<ull> {vec[i]});
}
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/08/25/PS/BOJ/16936/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.