Codeforces Round #514 (Div. 2) Sequence Transformation

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 𝑛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,,𝑛1,2,…,n. Find the lexicographically maximum result of its transformation.

A sequence 𝑎1,𝑎2,,𝑎𝑛a1,a2,…,an is lexicographically larger than a sequence 𝑏1,𝑏2,,𝑏𝑛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𝑛1061≤n≤106).

Output

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

题意:

  有一个1~n的序列,进行下列操作,求n个数的gcd,将此时gcd加入到答案序列中,再任意删除给出的序列中的一个数,直到原序列中没数为止。求字典序最大的答案序列。

分析:

  由于一开始的序列有奇数也有偶数,为了使得字典序最大,就必须先删除奇数,留下2的倍数,使得gcd变为2,并使得2尽快出现在答案中。

  然后再让gcd变为4,留下4的倍数删除其它。

  通过观察规律

  n=5

  1 2 3 4 5    5个数

  2 4             2个数

       4                1个数

  n=16

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16    16个数

  2 4 6 8 10 12 14 16                                   8个数

  4 8 12 16                                                   4个数

  8 16                                                           2个数

  16                                                              1个数

 

  所以答案序列中加入的数一定是(1,2,4,8,16........)

  通过给出样例观察出n=3的时候,对于序列1,2,3,最优的选择不是删除1,3留下2使gcd变为2,而是删除1,2使gcd变为3。

  

///  author:Kissheart  ///
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#define INF 0x3f3f3f3f
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
#define gcd(a,b) __gcd(a,b)
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
int n;
int main()
{
    int k=1;
    scanf("%d",&n);
    while(n)
    {
        if(n==3)
        {
            printf("%d %d %d ",k,k,k*3);
            k=k*2;
            n=n/2;
            break;
        }
        for(int i=1;i<=n-n/2;i++)
            printf("%d ",k);
        k=k*2;
        n=n/2;
    }
    return 0;

}
View Code

 

  

  

posted @ 2018-11-07 11:07  Kissheart_LMC  阅读(215)  评论(0)    收藏  举报