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 jbiai>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 
#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;
}

 

你可能感兴趣的:(Codeforces)