http://codeforces.com/problemset/problem/1059/C
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
3
Output
1 1 3
Input
2
Output
1 2
Input
1
Output
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.
题意:
从给你数N,从1到N,每次从中删一个数,求剩余数的GCD,每次操作的结果GCD的排序,尽量字典序最大。(第一个GCD是所有数的GCD,即‘1’)
思路:
首先肯定要删除1,不然,怎么删GCD都是1,不符合字典序最大。然后来几个小的数推一下,因为要字典序最大,所以要尽快让GCD不是1,很快就能发现,让GCD变2比变成其他的数来的快。如果继续按照这个思路,后面的操作也是一样的。但是感觉不太好证明。注意,剩下3个数的时候要特殊处理一下,例如n==3,1 1 2肯定比 1 1 3小。
代码
#include <cstdio>
#include <algorithm>
using namespace std;
int n,gcd=1;
int main()
{
scanf("%d",&n);
while(n){
if(n==3){
printf("%d %d %d\n",gcd,gcd,3*gcd);
return 0;
}
if(n==1){
printf("%d\n",gcd);
return 0;
}
for(int i=1;i<=n/2+n%2;i++) printf("%d ",gcd);
gcd<<=1;
n/=2;
}
return 0;
}