Description
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,…,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 nn (1≤n≤10^6).
Output
Output nn integers — the lexicographically maximum result of the transformation.
Sample Input
Input
3
Output
1 1 3
Input
2
Output
1 2
Input
1
Output
1
Hint
In the first sample the answer may be achieved this way:
- Append GCD(1,2,3)=1, remove 2.
- Append GCD(1,3)=1, remove 1.
- Append GCD(3)=3, remove 3.
We get the sequence [1,1,3]as the result.
题解:一开始的gcd肯定是1,要让字典序最大,我们可以想到下一个应该是2。这样就要把所有的奇数全给删去,这样就要考虑一个特殊情况,就是把所有奇数删去之后,刚好n==1的时候。因为n==1的话,gcd就是剩下的那个数本身了。因此要特判n==3的情况。(不少人认为最后一个数为n,如果为奇数,一定不成立。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<algorithm>
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define swap(a,b) (a=a+b,b=a-b,a=a-b)
#define maxn 320007
#define N 100000000
#define INF 0x3f3f3f3f
#define mod 1000000009
#define e 2.718281828459045
#define eps 1.0e18
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define memset(x,y) memset(x,y,sizeof(x))
#define Debug(x) cout<<x<<" "<<endl
#define lson i << 1,l,m
#define rson i << 1 | 1,m + 1,r
#define ll long long
//std::ios::sync_with_stdio(false);
//cin.tie(NULL);
using namespace std;
int main()
{
int n;
cin>>n;
int k=1;
while(n)
{
if(n==3)
{
cout<<k<<" "<<k<<" "<<k*3<<endl;
return 0;
}
for(int i=0; i<(n+1)/2; i++)
cout<<k<<" ";
n/=2;
k*=2;
}
return 0;
}