Codeforces Round #514 (Div. 2) C. Sequence Transformation

C. Sequence Transformation

 

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 (1≤n≤1061≤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 

题意:现在有一个1~n的序列,每次可以删一个数,剩余的所有数会产生一个gcd,要求你以某种删除方式,使得生成的gcd序列字典序最大。

思路:找个长一点的序列推一推就能推出来规律了。

经过手动模拟,发现想要使字典序最大,每次应该找到最长的子序列,且子序列gcd大于当前gcd

1.例如:1 2 3 4 5 6 7 8 9 10       毫无疑问,最长且gcd不是1的是什么序列?   必然是偶数序列,2,4,6,8,10。

当然会发现4,8的gcd更大,但是不要忘了,我们是依次一个一个删除的,数位越高越重要啊,现在百位能变大,非要换取个位变大的吗。

所以我们一开始一定留下所有的偶数序列,即删去1 3 5 7 9,gcd序列构造1 1 1 1 1。

2.接着我们观察2 4 6 8 10,继续找最长序列嘛,很明显删去2 6 10,(10删除不影响我们字典序最大,参考上面数位重要性)。

到此为止发现了什么,n/2个数留下,接着n/2/2个数留下,ok就是这样,每次删去一半,我们的gcd每次2倍。

尤其注意最后剩3个的情况,这个细节实在难以表达,自己推吧。

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n;
int main()
{
    while(~scanf("%d",&n))
    {
        int d=1,x;
        int n1=n;
        while(n)
        {
            if(n==3)   //剩三个时特判,规律推推就明白了
            {
                printf("%d %d %d",d,d,d*3);
                break;
            }
            if(n%2==0)x=n/2;   //x代表需要删除的个数
            else x=n/2+1;
            for(int i=1;i<=x;i++)printf("%d ",d);
            n-=x;   //保留的个数
            d*=2;   //gcd加倍
        }
        printf("\n");
    }
    return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值