codeforce-----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.InputThe 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.OutputPrint 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.Examples Input 6
4 8 6 3 12 9
Output 9 3 6 12 4 8
Input 4
42 28 84 126
Output 126 42 84 28
Input 2
1000000000000000000 3000000000000000000
Output 3000000000000000000 1000000000000000000
NoteIn 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/3,要么是前一个数的2倍

思路一:找规律,最后的序列保证前面的数的3的因子一定比后面的数多,如果相同的话,小的数在前面,因为保证一定有解

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 110;
typedef long long LL;
int n;
LL a[N];
int get_3(LL x){
 int cnt = 0;
 while(x % 3 == 0)   x /= 3,cnt ++;
 return cnt;
}
bool cmp(LL a, LL b){
 int x = get_3(a), y = get_3(b);
 if (x == y)   return a < b;
 return x > y;
}
int main(){
 int n;
 cin >> n;
 for (int i = 1; i <= n; i ++)
 cin >> a[i];
 sort(a + 1, a + n + 1, cmp);
 for (int i = 1; i <= n ;i ++)
 if (i != n)
 cout << a[i] << ' ';
 else
 cout << a[i] << endl;
  return 0;
}

思路二:dfs暴力枚举

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 110;
typedef long long LL;
LL a[N], ans[N];
int vis[N], n;
int flag;
void print(){
 for (int i = 1; i <= n ;i ++)   printf("%lld ", ans[i]);
}
void dfs(int x, int step){
 if (step == n && !flag){
  flag = 1;
  print();
  return ;
 }
  vis[x] = 1;
 for (int i = 1; i <= n; i ++){
  if (!vis[i] && (a[i] * 3 == ans[step] || ans[step] * 2 == a[i])){
  ans[step + 1] = a[i];
  dfs(i, step + 1);
 }
 }
 vis[x] = 0;
}
int main(){
 cin >> n;
 for (int i = 1; i <= n; i ++)   cin >> a[i];
 for (int i = 1; i <= n ;i ++){
  ans[1] = a[i];
  memset(vis, 0, sizeof vis);
  dfs(i, 1);
 }
  return 0;
}
发布了106 篇原创文章 · 获赞 67 · 访问量 5409

猜你喜欢

转载自blog.csdn.net/qq_45772483/article/details/105050962