Divide by three, multiply by two
Polycarp likes to play with numbers. He takes some integer number xx, writes it down on the board, and then performs with it n−1n−1 operations of the two kinds:
- divide the number xx by 33 (xx must be divisible by 33);
- multiply the number xx by 22.
After each operation, Polycarp writes down the result on the board and replaces xx by the result. So there will be nn numbers on the board after all.
You are given a sequence of length nn — the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
The first line of the input contatins an integer number nn (2≤n≤1002≤n≤100) — the number of the elements in the sequence. The second line of the input contains nn integer numbers a1,a2,…,ana1,a2,…,an (1≤ai≤3⋅10181≤ai≤3⋅1018) — rearranged (reordered) sequence that Polycarp can wrote down on the board.
Print nn integer numbers — rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
6
4 8 6 3 12 9
9 3 6 12 4 8
4
42 28 84 126
126 42 84 28
2
1000000000000000000 3000000000000000000
3000000000000000000 1000000000000000000
In the first example the given sequence can be rearranged in the following way: [9,3,6,12,4,8][9,3,6,12,4,8]. It can match possible Polycarp's game which started with x=9x=9:.
题解:找每个点的后继
1 #pragma warning(disable:4996) 2 #include<cmath> 3 #include<queue> 4 #include<map> 5 #include<vector> 6 #include<cstdio> 7 #include<cstring> 8 #include<iostream> 9 #include<algorithm> 10 using namespace std; 11 #define ll long long 12 13 map<ll, int> p; 14 vector<ll> v; 15 16 const int maxn = 105; 17 18 ll n, m; 19 ll a[maxn]; 20 21 bool DFS(ll num, int dep) { 22 bool res = false; 23 if (dep == n) { 24 v.push_back(num); 25 return true; 26 } 27 if (num % 3 == 0 && p[num / 3]) { 28 if (DFS(num / 3, dep + 1)) { 29 v.push_back(num); 30 res = true; 31 } 32 } 33 if (num * 2 <= m && p[num * 2]) { 34 if (DFS(num * 2, dep + 1)) { 35 v.push_back(num); 36 res = true; 37 } 38 } 39 return res; 40 } 41 42 int main() 43 { 44 while (cin >> n) { 45 m = 0; 46 p.clear(); 47 v.clear(); 48 for (int i = 1; i <= n; i++) { 49 cin >> a[i]; 50 p[a[i]]++; 51 m = max(m, a[i]); 52 } 53 for (int i = 1; i <= n; i++) { 54 if (DFS(a[i], 1)) break; 55 else v.clear(); 56 } 57 for (int i = n - 1; i >= 0; i--) cout << v[i] << endl; 58 } 59 return 0; 60 }
代码2:
1 #pragma warning(disable:4996) 2 #include<map> 3 #include<cstdio> 4 #include<cstring> 5 #include<iostream> 6 #include<algorithm> 7 using namespace std; 8 #define ll long long 9 10 map<ll, int> p; 11 12 const int maxn = 105; 13 14 ll n, m; 15 ll a[maxn], ans[maxn]; 16 bool flag; 17 18 void DFS(ll num, int dep) { 19 ans[dep] = num; 20 if (dep == n) { 21 flag = true; 22 return; 23 } 24 if (num % 3 == 0 && p[num / 3]) DFS(num / 3, dep + 1); 25 if (num * 2 <= m && p[num * 2]) DFS(num * 2, dep + 1); 26 return; 27 } 28 29 int main() 30 { 31 while (cin >> n) { 32 m = 0; 33 p.clear(); 34 for (int i = 1; i <= n; i++) { 35 cin >> a[i]; 36 p[a[i]]++; 37 m = max(m, a[i]); 38 } 39 for (int i = 1; i <= n; i++) { 40 flag = false; 41 DFS(a[i], 1); 42 if (flag) break; 43 } 44 for (int i = 1; i <= n; i++) { 45 cout << ans[i] << endl; 46 } 47 } 48 return 0; 49 }