题目C--Competition - 20181010

题目

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 这个序列中所有元素的最大公因数,随机去掉一个数,再得到剩余序列中所有元素的最大公因数,直至剩余序列中元素为0,得到一个最大公因数组成的序列,(该序列由于删除元素的顺序不同,有多种可能),求字典序最大的那个最大公因数组成的序列。

思路:看懂了题目,没有任何思路,试着将前面几组写出来,发现貌似去除奇数可以让序列的最大公因数尽早脱离“1”,达到字典序最大的要求。

枚举样例是我最喜欢的戳记不会题目的方式,在写出结果 的过程中,你就是那个最笨蛋的计算机在执行步骤,还是挺容易发现规律的,(下面这个不是我写的,但十分符合我笨蛋的步骤口味):

手动模拟出答案:

/*
    1
    1 2
    1 1 3
    1 1 2 4
    1 1 1 2 4
    1 1 1 2 2 6
    1 1 1 1 2 2 6
    1 1 1 1 2 2 4 8
    1 1 1 1 1 2 2 4 8
    1 1 1 1 1 2 2 2 4 8
    1 1 1 1 1 1 2 2 2 4 8
    1 1 1 1 1 1 2 2 2 4 4 12
    1 1 1 1 1 1 1 2 2 2 4 4 12
    1 1 1 1 1 1 1 2 2 2 2 4 4 12
    1 1 1 1 1 1 1 1 2 2 2 2 4 4 12
    1 1 1 1 1 1 1 1 2 2 2 2 4 4 8 16
*/

隐隐约约看出了一丝杀机,貌似是二分,但是,3,6,12的存在否定了这个暴力列举,然后就卡住了,找到了一个类似于这种枚举方式的代码,但很聪明,对于n==3的时候,有一个特殊处理方式,代码如下:

/*
思路:
一开始的gcd肯定是1,要让字典序最大,我们可以想到下一个应该是2。
这样就要把所有的奇数全给删去,这样就要考虑一个特殊情况,
就是把所有奇数删去之后,刚好n==1的时候。
因为n==1的话,gcd就是剩下的那个数本身了。
因此要特判n==3的情况。
(不少人认为最后一个数为n,如果为奇数,一定不成立。
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<algorithm>

using namespace std;
 
int main()
{
    int n;
    cin>>n;
    int k=1;
    while(n)
    {
        if(n==3)
        {
            cout<<k<<" "<<k<<" "<<k*3<<endl;
            return 0;
        }
        for(int i=0; i<(n+1)/2; i++)
            cout<<k<<" ";
        n/=2;
        k*=2;
    }
    return 0;
}

还有一种正正经经处理的方式:

/*
尽可能的让大的gcd值尽快出现。

有一条规则可以推出来,
两个连续的数的gcd是1,所以第一步是将原数列变成奇数数列或偶数数列,
又因为对于长度n大于3时,
偶数数列肯定要先出现大的gcd,所以第一步将原数列转成偶数数列。

之后有趣的事情就出现了,可以发现,
可以将形成的数列,奇数位上的数看“奇数数列”,偶数位上的数看成“偶数数列”,
又重复第一步的过程。

在以上整个程中n都是大于3的,对于小于3的直接按“偶奇奇”的顺序删。
*/

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<iostream>
using namespace std;
typedef long long  LL;
const double pi=acos(-1.0);
const double e=exp(1);
const int N = 100009;

int con[1000009];

int gcd(int a,int b)
{
    int c;
    while(b)
    {
        c=b;
        b=a%b;
        a=c;
    }
    return a;
}

int main()
{
    int i,p,j,n;
    int cnt=0;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
        con[i]=i;
    p=n;
    while(p>0)
    {
        if(p==3)
        {
            printf("%d %d %d\n",gcd(gcd(con[1],con[2]),con[3]),gcd(con[2],con[3]),con[3]);
            break;
        }
        else if(p>=2)
        {
            cnt=0;
            int k=gcd(con[1],con[2]);
            for(i=1;i<=p;i+=2)
            {
                printf("%d ",k);
                if(i+1<=p)
                    con[++cnt]=con[i+1];
            }
            p=cnt;
        }
        else if(p==1)
        {
            printf("%d\n",con[p]);
            break;
        }

    }
    return 0;
}

比较可惜的是自己没能坚持思考下去(毕竟读懂一道题不容易),最开始的方向是有可能得到解的。

猜你喜欢

转载自blog.csdn.net/sodacoco/article/details/83019607