今日水题博客第二篇(共十篇) CodeForces - 1059C

博客围绕整数序列变换展开,给定整数序列1到n,每次删除一个数并记录剩余数的最大公约数(GCD),要求得到字典序最大的结果序列。介绍了思路,如先删除1,让GCD尽快不为1,还提到剩余3个数时需特殊处理,并给出了示例和代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}                     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值