CF1059C Sequence Transformation 题解

Posted qq965921539

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF1059C Sequence Transformation 题解相关的知识,希望对你有一定的参考价值。

 

这几天不知道写点什么,状态也不太好,搬个题上来吧

题意:给定一个数n,设一个从1到n的序列,每次删掉一个序列中的数,求按字典序最大化的GCD序列

做法:按2的倍数找,但是如果除2能得到3的这种情况要特殊处理(¥#……%¥……@#¥不知道该怎么描述,看代码吧)

C. Sequence Transformation
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Let‘s call the following process a transformation of a sequence of length nn.

If the sequence is empty, the process ends. Otherwise, append the greatest common divisor (GCD) of all the elements of the sequence to the result and remove one arbitrary element from the sequence. Thus, when the process ends, we have a sequence of nn integers: the greatest common divisors of all the elements in the sequence before each deletion.

You are given an integer sequence 1,2,,n1,2,…,n. Find the lexicographically maximum result of its transformation.

A sequence a1,a2,,ana1,a2,…,an is lexicographically larger than a sequence b1,b2,,bnb1,b2,…,bn, if there is an index ii such that aj=bjaj=bj for all j<ij<i, and ai>biai>bi.

Input

The first and only line of input contains one integer nn (1n1061≤n≤106).

Output

Output nn integers  — the lexicographically maximum result of the transformation.

Examples
input
Copy
3
output
Copy
1 1 3 
input
Copy
2
output
Copy
1 2 
input
Copy
1
output
Copy
1 
Note

In the first sample the answer may be achieved this way:

  • Append GCD(1,2,3)=1(1,2,3)=1, remove 22.
  • Append GCD(1,3)=1(1,3)=1, remove 11.
  • Append GCD(3)=3(3)=3, remove 33.

We get the sequence [1,1,3][1,1,3] as the result.

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int p[25];
 5 
 6 int _pow(int a, int b)
 7 {
 8     int ans = 1;
 9     int temp = a;
10     while (b)
11     {
12         if (b & 1)
13             ans *= temp;
14         temp *= temp;
15         b >>= 1;
16     }
17     return ans;
18 }
19 
20 int main()
21 {
22     ios::sync_with_stdio(false);
23     cout.tie(0);
24     for (int i = 0; i <= 20; i++)
25     {
26         p[i] = _pow(2, i);
27     }
28 
29     int n;
30     cin >> n;
31     if (n == 3)
32     {
33         cout << "1 1 3" << endl;
34         return 0;
35     }
36 
37     int step = 0;
38     int flag = 0;
39     int n1 = n;
40     while (n1)
41     {
42         if (n1 == 3 && !flag)
43         {
44             int temp = 6;
45             while (temp <= n)
46             {
47                 temp *= 2;
48             }
49             flag = temp / 2;
50             break;
51         }
52         n1 /= 2;
53     }
54     while (n)
55     {
56         int num = n - n / 2;
57         n -= num;
58 
59         for (int i = 0; i < num; i++)
60         {
61             if (flag && num == 1)
62                 cout << flag;
63             else
64                 cout << p[step];
65             if (i != num - 1 || n)
66                 cout << " ";
67         }
68         step++;
69     }
70 
71     cout << endl;
72     return 0;
73 }

 

以上是关于CF1059C Sequence Transformation 题解的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces 1059C

cf487C Prefix Product Sequence

CF 286C(Main Sequence-贪心括号匹配)

cf670E Correct Bracket Sequence Editor

IncDec Sequence (差分)

CF623E Transforming Sequence