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
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
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
3Output
1 1 3Input
2Output
1 2Input
1Output
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\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 }View Code
CodeForces 1059C