A - Sequence Transformation CodeForces - 1059C(递归)

Let’s call the following process a transformation of a sequence of length 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 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 n (1≤n≤106).

Output
Output 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, 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.

题意:给出一个数n,代表一个序列1-n,如n=5,序列为1,2,3,4,5
要求每次删除一个数求gcd,这样会得到一个长度为n的gcd序列,要求这个gcd序列最大

思路:自己模拟一遍,可以发现,要想尽快得到大的gcd,就要先把奇数删除掉,然后得到gcd=2
然后在剩下的数中,又可以发现规律,把2的奇数倍删除会得到gcd=4,以此类推 删4奇数倍得到8
举例:n=8
第一次 1 2 3 4 5 6 7 8
把奇数删除
序列变成 : 2 4 6 8
gcd序列为:1 1 1 1
接下来删除2的奇数倍
序列变为: 4 8
gcd序列变为 :1 1 1 1 2 2
剩下两个数就可以直接求了,必然就是4 8
gcd序列变为:1 1 1 1 2 2 4 8
开始不知道怎么写,只知道思路,看到dalao写的递归,很强
代码如下:

#include<bits/stdc++.h>
#define LL long long
#define Max 1000005
#define Mod 1e9+7
const LL mod=1e9+7;
const LL inf=0x3f3f3f3f;
using namespace std;
int n,a[Max],ans[Max],c=0;
void fun(int n,int t)
{
    if(n==1){
        ans[c++]=t;
        return;
    }//n=1,2,3可以直接求
    if(n==2){
        ans[c++]=t,ans[c++]=t*2;
        return ;
    }
    if(n==3){
        ans[c++]=t,ans[c++]=t;ans[c++]=t*3;
        return ;
    }
    for(int i=0;i<n;i++)
        if((a[i]/t)&1)//如果是t的奇数倍,gcd就是t
            ans[c++]=t;
    for(int i=0;i<n/2;i++)
        a[i]=a[2*i+1];//将偶数倍的数加入数组
    fun(n/2,t*2);
}
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        a[i]=i+1;
    fun(n,1);
    for(int i=0;i<c;i++)
        printf("%d ",ans[i]);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值