CF1059C Sequence Transformation 题解 思维

Sequence Transformation

传送门

Let’s call the following process a transformation of a sequence of length n n 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 n 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 1, 2, \dots, n 1,2,,n. Find the lexicographically maximum result of its transformation.

A sequence a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an is lexicographically larger than a sequence b 1 , b 2 , … , b n b_1, b_2, \ldots, b_n b1,b2,,bn, if there is an index i i i such that a j = b j a_j = b_j aj=bj for all j < i j < i j<i, and a i > b i a_i > b_i ai>bi.

Input

The first and only line of input contains one integer n n n ( 1 ≤ n ≤ 1 0 6 1\le n\le 10^6 1n106).

Output

Output n n 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

Note

In the first sample the answer may be achieved this way:

  • Append GCD ( 1 , 2 , 3 ) = 1 (1, 2, 3) = 1 (1,2,3)=1, remove 2 2 2.
  • Append GCD ( 1 , 3 ) = 1 (1, 3) = 1 (1,3)=1, remove 1 1 1.
  • Append GCD ( 3 ) = 3 (3) = 3 (3)=3, remove 3 3 3.

We get the sequence [ 1 , 1 , 3 ] [1, 1, 3] [1,1,3] as the result.

题目翻译

我们把下面的过程称为长度为 n n n 的序列的变换。

如果序列为空,则过程结束。否则,将序列中所有元素的 最大公约数 (GCD) 附加到结果中,并从序列中删除一个任意元素。这样,当过程结束时,我们就得到了一个由 n n n 个整数组成的序列:每次删除前序列中所有元素的最大公约数。

给你一个整数序列 1 , 2 , … , n 1, 2, \dots, n 1,2,,n 。求其变换的最大词性结果。

如果存在一个索引 i i i ,使得所有 j < i j < i j<i a i > b i a_i > b_i ai>bi 都是 a j = b j a_j = b_j aj=bj ,那么序列 a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an 在词典上大于序列 b 1 , b 2 , … , b n b_1, b_2, \ldots, b_n b1,b2,,bn

输入

第一行也是唯一一行输入包含一个整数 n n n ( 1 ≤ n ≤ 1 0 6 1\le n\le 10^6 1n106 )。

输出

输出 n n n 个整数–转换结果的最大值。

提示

在第一个示例中,答案可以这样得到:

  • 追加 GCD ( 1 , 2 , 3 ) = 1 (1, 2, 3) = 1 (1,2,3)=1 ,删除 2 2 2
  • 追加 GCD ( 1 , 3 ) = 1 (1, 3) = 1 (1,3)=1 ,删除 1 1 1
  • 追加 GCD ( 3 ) = 3 (3) = 3 (3)=3 ,删除 3 3 3

结果得到序列 [ 1 , 1 , 3 ] [1, 1, 3] [1,1,3]

注明

以上来自 C o d e F o r c e s ,翻译: D e e p L 以上来自CodeForces,翻译:DeepL 以上来自CodeForces,翻译:DeepL
个人认为洛谷翻译更好。

解题思路

显然,前 n 2 \frac{n}{2} 2n 个数都为 1 1 1

对于之后的答案,我们要使答案的字典序最大,那么就要使后续的序列 gcd ⁡ \gcd gcd 尽可能大。对于数列,我们有删奇数和删偶数两种选择:

  • 对于删偶数,则会使序列中有元素为 1 1 1 的情况,会使序列 gcd ⁡ \gcd gcd 1 1 1,不优;
  • 对于删奇数,则当奇数全部删完后,再每删一个当前最小的元素,会使序列的 gcd ⁡ \gcd gcd 变为原来的两倍。

所以先删奇数更优秀。
最后,照以上的描述进行实现即可。

然而,对于 n ≤ 3 n \le 3 n3 的输入,直接照搬样例输入输出即可。

AC Code

#include<bits/stdc++.h>
using namespace std;
char buf[1048576], *p1, *p2;
template<typename T>inline void Super_Quick_Read(T &x) {
	bool f = 1;
	x = 0;
	char ch = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? 0 : *p1++);
	while (ch < '0' || ch > '9') {
		if (ch == '-') f = !f;
		ch = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? 0 : *p1++);
	}
	while (ch >= '0' && ch <= '9')x = (x << 1) + (x << 3) + (ch ^ 48), ch = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? 0 : *p1++);
	x = (f ? x : -x);
	return;
}
template<typename T>inline void Quick_Write(T x) {
	if (x < 0) putchar('-'), x = -x;
	if (x > 9) Quick_Write(x / 10);
	putchar(x % 10 + '0');
	return;
}
int n;
int x = 1;
signed main() {
	Super_Quick_Read(n);
	while (n >= 4) {
		for (register int i = 0; i < (n + 1) >> 1; ++i) Quick_Write(x), puts(" ");
		n /= 2, x *= 2;
	}
	if (n == 3) printf("%d %d %d", x, x, x * 3);
	else if (n == 2) printf("%d %d", x, x * 2);
	else printf("%d", x);
	return 0;
}

重要提醒

洛谷好像要迁移到海外了,幻兽帕鲁害人不浅啊。

  • 26
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值