标签:html enc math text this format gif line close
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 (1≤n≤1061≤n≤106 ).
Output
Output nn integers — the lexicographically maximum result of the transformation.
Sample Input
3
1 1 3
2
1 2
1
1
Sample Output
Hint
In the first sample the answer may be achieved this way:
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\n",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\n",con[p]); 62 break; 63 } 64 65 } 66 return 0; 67 }
标签:html enc math text this format gif line close
原文地址:https://www.cnblogs.com/daybreaking/p/9751744.html