CodeForces 1059 C.Sequence Transformation (思维)

C.Sequence Transformation

Let’s call the following process a transformation of a sequence of length n.

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 n integers: the greatest common divisors of all the elements in the sequence before each deletion.

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

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

Input

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

Output

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

Examples

Input

3

Output

1 1 3

Input

2

Output

1 2

Input

1

Output

1

题意:

给n
开始有一个1,2,3...n的排列
重复一下操作:
1.输出整个数组的gcd
2.任意删掉一个数

问输出的gcd数组,字典序最大的结果是什么

思路:

因为题目要求字典序最大,字典序最大需要让不为1的gcd出现的越早越好
我们知道相邻的两个数互斥
所以要使得gcd不为1需要把相邻的数拆开,即删掉全部奇数或者全部偶数
我们又知道奇数集合也是互斥的(因为奇数里面的质数很多3,5,7,偶数里面只有一个质数2)
因此先删掉全部奇数
奇数个数为n-n/2,所以先输出n-n/21
然后序列变为2,4,6,8,10...
gcd为2,这个序列可以看成1*2,2*2,3*2....其实就是2*(1,2,3....)长度为n/2
就是原问题的子问题,只是序列的长度缩短了,所以只需要和上面一样重复操作直到序列长度小于等于3

当序列长度小于等于3的时候,其实题目已经把方案全部给出来了
特判一下长度然后照着题目输出即可

code:

#include<bits/stdc++.h>
using namespace std;
const int maxm=1e6+5;
int n;
signed main(){
    cin>>n;
    int t=1;
    while(n>3){
        for(int i=1;i<=n-n/2;i++){
            cout<<t<<' ';
        }
        n/=2;
        t*=2;
    }
    if(n==3)cout<<t<<' '<<t<<' '<<t*3<<endl;//照抄题目样例即可
    else if(n==2)cout<<t<<' '<<t*2<<endl;
    else cout<<t<<endl;
    return 0;
}
发布了378 篇原创文章 · 获赞 29 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_44178736/article/details/104094996
今日推荐