1. 程式人生 > >codeforces 1059C. Sequence Transformation【構造】

codeforces 1059C. Sequence Transformation【構造】

ati pro using 奇數 sed orm 思路 字典序 分享圖片

題目:戳這裏

題意:有1,2,3...n這n個數,求一次這些數的gcd,刪去一個數,直到剩下一個數為止。輸出這n個gcd的最大字典序。

解題思路:一開始的gcd肯定是1,要讓字典序最大,我們可以想到下一個應該是2。這樣就要把所有的奇數全給刪去,這樣就要考慮一個特殊情況,就是把所有奇數刪去之後,剛好n==1的時候。因為n==1的話,gcd就是剩下的那個數本身了。因此要特判n==3的情況。其他的時候循環刪除奇數的操作。(要不是b題看不懂題意,這次也不會那麽慘T T

具體看代碼。

技術分享圖片
 1 #include <bits/stdc++.h>
 2 typedef long long ll;
3 const int maxn = 1e6 + 10; 4 const int seed = 131; 5 const ll mod = 998244353; 6 const int inf = 0x3f3f3f3f; 7 using namespace std; 8 int main(){ 9 10 int n, cnt = 1; 11 scanf("%d", &n); 12 while(n) 13 { 14 if(n == 3) 15 { 16 printf("%d %d %d", cnt, cnt, cnt * 3
); 17 return 0; 18 } 19 for(int i = 1; i <= n / 2 + n % 2; ++i) printf("%d ", cnt); 20 n /= 2; 21 cnt *= 2; 22 } 23 return 0; 24 }
View Code

codeforces 1059C. Sequence Transformation【構造】