CF1059C Sequence Transformation 題解
這幾天不知道寫點什麽,狀態也不太好,搬個題上來吧
題意:給定一個數n,設一個從1到n的序列,每次刪掉一個序列中的數,求按字典序最大化的GCD序列
做法:按2的倍數找,但是如果除2能得到3的這種情況要特殊處理(¥#……%¥……@#¥不知道該怎麽描述,看代碼吧)
C. Sequence Transformation time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard outputLet‘s call the following process a transformation of a sequence of length
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
The first and only line of input contains one integer nn (1≤n≤1061≤n≤106).
OutputOutput nn integers — the lexicographically maximum result of the transformation.
Examples input Copy3output Copy
1 1 3input Copy
2output Copy
1 2input Copy
1output Copy
1Note
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 #include <iostream> 2 using namespace std; 3 4 int p[25]; 5 6 int _pow(int a, int b) 7 { 8 int ans = 1; 9 int temp = a; 10 while (b) 11 { 12 if (b & 1) 13 ans *= temp; 14 temp *= temp; 15 b >>= 1; 16 } 17 return ans; 18 } 19 20 int main() 21 { 22 ios::sync_with_stdio(false); 23 cout.tie(0); 24 for (int i = 0; i <= 20; i++) 25 { 26 p[i] = _pow(2, i); 27 } 28 29 int n; 30 cin >> n; 31 if (n == 3) 32 { 33 cout << "1 1 3" << endl; 34 return 0; 35 } 36 37 int step = 0; 38 int flag = 0; 39 int n1 = n; 40 while (n1) 41 { 42 if (n1 == 3 && !flag) 43 { 44 int temp = 6; 45 while (temp <= n) 46 { 47 temp *= 2; 48 } 49 flag = temp / 2; 50 break; 51 } 52 n1 /= 2; 53 } 54 while (n) 55 { 56 int num = n - n / 2; 57 n -= num; 58 59 for (int i = 0; i < num; i++) 60 { 61 if (flag && num == 1) 62 cout << flag; 63 else 64 cout << p[step]; 65 if (i != num - 1 || n) 66 cout << " "; 67 } 68 step++; 69 } 70 71 cout << endl; 72 return 0; 73 }
CF1059C Sequence Transformation 題解