Divide by three, multiply by two CodeForces - 977D(搜索||规律)
Polycarp likes to play with numbers. He takes some integer number x, writes it down on the board, and then performs with it n−1
operations of the two kinds:
- divide the number x
by 3 (x must be divisible by 3
- );
- multiply the number x
by 2
- .
After each operation, Polycarp writes down the result on the board and replaces x
by the result. So there will be n
numbers on the board after all.
You are given a sequence of length n
— 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.
Input
The first line of the input contatins an integer number n
(2≤n≤100) — the number of the elements in the sequence. The second line of the input contains n integer numbers a1,a2,…,an (1≤ai≤3⋅1018
) — rearranged (reordered) sequence that Polycarp can wrote down on the board.
Output
Print n
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
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
Note
In the first example the given sequence can be rearranged in the following way: [9,3,6,12,4,8]
. It can match possible Polycarp's game which started with x=9.
题目大意:就是给一个数组,给这个数组重新排序,使它满足这样一个条件a[i] = a[i-1]*2 或者 a[i-1] = a[i]*3;
思路:1. 爆搜不解释 2.规律 :可以把数组里的数分两类:3的倍数 和不是3的倍数 如果是3的倍数 则按照可以除以3的次数排序
,次数相等按照数值大小排序.,如果不是3的倍数按照对3取余按余数排序 余数相等按数值大小排序.
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 #define ll unsigned long long 7 #define inx 210 8 using namespace std; 9 int n, s, vis[inx]; 10 ll a[inx], b[inx]; 11 void dfs() 12 { 13 if(s == n+1) 14 { 15 for(int i=1; i<s;i++) 16 printf("%lld%c",b[i],i==s?'\n':' '); 17 } 18 for(int i=1; i<=n;i++) 19 { 20 if(vis[i] == 0&&(a[i]==b[s-1]*2||a[i]*3==b[s-1])) 21 { 22 vis[i] = 1; 23 b[s++] = a[i]; 24 dfs(); 25 s--; 26 vis[i] = 0; 27 } 28 } 29 } 30 int main() 31 { 32 while(~scanf("%d", &n)) 33 { 34 for(int i=1; i<=n; i++) 35 cin>>a[i]; 36 memset(vis, 0, sizeof(vis)); 37 for(int i=1; i<=n ;i++) 38 { 39 s = 1; 40 b[s++] = a[i]; 41 vis[i] = 1; 42 dfs(); 43 vis[i] = 0; 44 } 45 } 46 return 0; 47 }
1 #include <iostream> 2 #include<stdio.h> 3 #include<math.h> 4 #include<string.h> 5 #include <algorithm> 6 using namespace std; 7 #define ll unsigned long long 8 ll a[102]; 9 ll gg(ll x) 10 { 11 int nn=0; 12 while(x%3==0) 13 { 14 nn++; 15 x/=3; 16 } 17 return nn; 18 } 19 bool cmp(ll a,ll b) 20 { 21 ll x=gg(a),y=gg(b); 22 if(x==y) 23 return a<b; 24 return x>y; 25 } 26 int main() 27 { 28 int n; 29 cin>>n; 30 for(int i=0; i<n; i++) 31 scanf("%lld", &a[i]); 32 sort(a, a+n,cmp); 33 for(ll i=0; i<n; i++) 34 printf("%lld%c",a[i],i==n-1?'\n':' '); 35 return 0; 36 37 }