luogu#P5080. Tweetuzki 爱序列

Tweetuzki 爱序列

Background

This problem is an adapted problem.

Problem Description

Tweetuzki has a sequence a1,a2,,ana_1, a_2, \cdots, a_n of length nn.

He wants to find the maximum kk such that there exist some numbers b1,b2,,bkb_1, b_2, \cdots, b_k in the original sequence (not necessarily in the same order as in the original sequence), satisfying that for any i(1i<k)i(1 \le i < k), either bi÷3=bi+1b_i \div 3 = b_{i+1} (in this case, bib_i must be divisible by 33) or bi×2=bi+1b_i \times 2 = b_{i+1}. Output this sequence as well.

Input Format

The first line contains a positive integer n(2n105)n(2 \le n \le 10^5), representing the length of the sequence.
The second line contains nn positive integers $a_1, a_2, \cdots, a_n(1 \le a_i \le 3 \times 10^{18})$, describing the sequence.

Output Format

The first line contains a positive integer kk, representing the maximum kk.
The second line contains kk positive integers b1,b2,,bkb_1, b_2, \cdots, b_k, describing the numbers you chose.

6
4 8 6 3 12 9

6
9 3 6 12 4 8 

4
42 28 84 126

4
126 42 84 28 

5
4 8 16 12 24
4
12 24 8 16

Hint

Subtask #1 (20 points)2n82 \le n \le 8
Subtask #2 (30 points)2n100,1ai7×1082 \le n \le 100, 1 \le a_i \le 7 \times 10^8
Subtask #3 (20 points)2n1000,1ai10002 \le n \le 1000, 1 \le a_i \le 1000
Subtask #4 (30 points)2n105,1ai3×10182 \le n \le 10^5, 1 \le a_i \le 3 \times 10^{18}

Translated by ChatGPT 5