CodeForces - 1059C Sequence Transformation (思维)

版权声明:Why is everything so heavy? https://blog.csdn.net/lzc504603913/article/details/82949878

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 (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 

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~N,N个数,每次可以删除一个数,在删除数之前,把他们的GCD算出来,记录到答案里。问最后删除完所有数后,答案列表的字典序最大的答案是多少

解题思路:我们模拟一下,由于要字典序最大,就会发现必须要贪心的尽快的把所有的奇数都去掉,使得2尽快的出现在答案里。我们继续模拟,就会发现,下一步是尽快的使得4出现在答案里,然后是8,然后是16……然后序列长度不够的时候要特殊判断。

手动模拟出答案!

/*
    1
    1 2
    1 1 3
    1 1 2 4
    1 1 1 2 4
    1 1 1 2 2 6
    1 1 1 1 2 2 6
    1 1 1 1 2 2 4 8
    1 1 1 1 1 2 2 4 8
    1 1 1 1 1 2 2 2 4 8
    1 1 1 1 1 1 2 2 2 4 8
    1 1 1 1 1 1 2 2 2 4 4 12
    1 1 1 1 1 1 1 2 2 2 4 4 12
    1 1 1 1 1 1 1 2 2 2 2 4 4 12
    1 1 1 1 1 1 1 1 2 2 2 2 4 4 12
    1 1 1 1 1 1 1 1 2 2 2 2 4 4 8 16

*/

#include <bits/stdc++.h>
using namespace std;
const int MAXN=1000005;
typedef long long ll;


vector<int> ans;
bool vis[MAXN];

int main(){

    int N;
    scanf("%d",&N);
    int cur=2;

    while(1){

        if(cur*2>N)
            break;
        for(int i=1;i<=N;i++){
            if(i%cur!=0&&vis[i]==0){
                ans.push_back(cur/2);
                vis[i]=1;
            }
        }
        cur*=2;
    }


    int last=0;
    for(int i=1;i<=N;i++){
        if(vis[i]==0){
            last=i;
            ans.push_back(cur/2);
            vis[i]=1;
        }
    }
    if(last!=0){
        ans.pop_back();
        ans.push_back(last);
    }

    for(auto a:ans)
        cout<<a<<" ";

    return 0;
}

猜你喜欢

转载自blog.csdn.net/lzc504603913/article/details/82949878