zoukankan      html  css  js  c++  java
  • CodeForces 1059C

    Description

    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 (1n1061≤n≤106 ).

    Output

    Output nn integers  — the lexicographically maximum result of the transformation.

    Sample Input

     
    Input
    3
    Output
    1 1 3 
    Input
    2
    Output
    1 2 
    Input
    1
    Output
    1 

    Sample Output

     

    Hint

    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.

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

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

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

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

     1 #include<cstdio>
     2 #include<cstdlib>
     3 #include<cstring>
     4 #include<string>
     5 #include<cmath>
     6 #include<algorithm>
     7 #include<queue>
     8 #include<stack>
     9 #include<deque>
    10 #include<map>
    11 #include<iostream>
    12 using namespace std;
    13 typedef long long  LL;
    14 const double pi=acos(-1.0);
    15 const double e=exp(1);
    16 const int N = 100009;
    17 
    18 int con[1000009];
    19 
    20 int gcd(int a,int b)
    21 {
    22     int c;
    23     while(b)
    24     {
    25         c=b;
    26         b=a%b;
    27         a=c;
    28     }
    29     return a;
    30 }
    31 
    32 int main()
    33 {
    34     int i,p,j,n;
    35     int cnt=0;
    36     scanf("%d",&n);
    37     for(i=1;i<=n;i++)
    38         con[i]=i;
    39     p=n;
    40     while(p>0)
    41     {
    42         if(p==3)
    43         {
    44             printf("%d %d %d
    ",gcd(gcd(con[1],con[2]),con[3]),gcd(con[2],con[3]),con[3]);
    45             break;
    46         }
    47         else if(p>=2)
    48         {
    49             cnt=0;
    50             int k=gcd(con[1],con[2]);
    51             for(i=1;i<=p;i+=2)
    52             {
    53                 printf("%d ",k);
    54                 if(i+1<=p)
    55                     con[++cnt]=con[i+1];
    56             }
    57             p=cnt;
    58         }
    59         else if(p==1)
    60         {
    61             printf("%d
    ",con[p]);
    62             break;
    63         }
    64 
    65     }
    66     return 0;
    67 }
    View Code
  • 相关阅读:
    JS来推断文本框内容改变事件
    LINQ To SQL 语法及实例大全
    linux-多线程
    BackTrack5 (BT5)无线password破解教程之WPA/WPA2-PSK型无线password破解
    用Jfree实现条形柱状图表,java代码实现
    OpenGL中glPushMatrix和glPopMatrix的原理
    C# 之 抽象类与接口
    Android漫游记(1)---内存映射镜像(memory maps)
    Web页面布局方式小结
    STL中主要的算法(一)
  • 原文地址:https://www.cnblogs.com/daybreaking/p/9751744.html
Copyright © 2011-2022 走看看