C. 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.
考虑规模为 kk 的一个问题:11 ~ kk 的一个排列,求字典序最大的答案序列。
当 k\le 3k≤3 时,就是样例;当 k>3k>3 时,为了使答案序列的字典序尽量大,需要删尽量少的数使得 gcd\neq 1gcd≠1,此时删掉所有奇数一定是最优的,因为对于任意的 k>3,x>2k>3,x>2 有 \small\left\lfloor\dfrac{k}{x}\right\rfloor<\left\lfloor\dfrac{k}{2}\right\rfloor⌊xk⌋<⌊2k⌋。删掉所有奇数后,剩下的数为都为 22 的倍数(废话...),那么如果将所有数除以二,最优的删数方案是不会变的,而此时剩余的问题被转化成了一个规模为 \small\left\lfloor\dfrac{k}{2}\right\rfloor⌊2k⌋ 的原问题,输出答案时将答案乘二即可。
换句话说,令 T(k)T(k) 为 1,2,3,\cdots,k1,2,3,⋯,k 这个序列的字典序最大的答案序列,那么:\begin{cases}T(k)=\{1\}\quad(k=1)\\T(k)=\{1,2\}\quad(k=2)\\T(k)=\{1,1,3\}\quad(k=3)\\T(k)=\{1,1,\cdots,1\}(\small\text{共}\left\lceil\dfrac{k}{2}\right\rceil\text{个 1})\normalsize+T\left(\small\left\lfloor\dfrac{k}{2}\right\rfloor\right)\text{的每一项乘二}\normalsize\quad(k>3)\end{cases}⎩⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎧T(k)={1}(k=1)T(k)={1,2}(k=2)T(k)={1,1,3}(k=3)T(k)={1,1,⋯,1}(共⌈2k⌉个 1)+T(⌊2k⌋)的每一项乘二(k>3)
实现时并不需要递归地实现,详见代码:
#include <iostream>
#include<stdio.h>
using namespace std;
int main()
{
int n,j;
cin>>n;
int qaq=1;
while(n>=4)
{
for(int i=0; i<(n+1)/2; i++)
printf("%d ",qaq);
n/=2;
qaq*=2;
}
if(n==3)
printf("%d %d %d\n",qaq,qaq,qaq*3);
else if(n==2)
printf("%d %d\n",qaq,qaq*2);
else printf("%d\n",qaq);
return 0;
}