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 }

 

转载于:https://www.cnblogs.com/qq965921539/p/9756418.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值