首页 > 其他分享 >Codeforces1059C. Sequence Transformation 好题 没做出来 数学思维

Codeforces1059C. Sequence Transformation 好题 没做出来 数学思维

时间:2023-02-07 17:04:21浏览次数:58  
标签:Codeforces1059C GCD sequence int Sequence 好题 remove 序列 include


 

C. Sequence Transformation

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Examples

input

Copy


3


output

Copy


1 1 3


input

Copy


2


output

Copy


1 2


input

Copy


1


output

Copy


1


Note

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.


题意:给定一个数n,设一个从1到n的序列,每次删掉一个序列中的数,求按字典序最大化的GCD序列

分析:首先有一个显而易见的结论:对于任意排列,b序列中的前n/2个数总是1。
原因是相邻的两个数互质。
现在我们来考虑一个长度大于3的排列,怎样才能将b序列的字典序最大。
根据字典序的定义,我们需要最大化每一次进入队列的gcd。
即,当前最大就是整体最好的选择。
那么我们对于任意两个相邻的数,都有一个选择:删除其中的奇数或删除其中的偶数。
我们应该全部删除奇数或者全部删除偶数,不然就会有两个数相邻,从而出现gcd=1使得答案不优。
如果我们删除偶数,我们就会发现有ai=1使得这个序列的gcd=1而使得答案不优。
所以我们对于每个序列,都要删除奇数,使得gcd*=2。
观察操作后的序列
    a{2,4,...,(n&1)?(n-1):(n)}
我们可以将它看成
    a{2*1,2*2,2*3,...,2*(n/2)}
这样我们就把问题转化成了一个长度为n/2的子问题。
对于1<=n<=3的序列或长度小于等于3的子问题,题目的样例就是我们想要的结果,特判即可。
复杂度实际上是O(n)的


代码实现:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long ll;
int main(){


int n;
cin>>n;
int k=1;
while(n)
{
if(n==3)
{
cout<<k<<" "<<k<<" "<<k*3<<endl;
return 0;
}
for(int i=0; i<(n+1)/2; i++)
cout<<k<<" ";
n/=2;
k*=2;
}
return 0;
}

 

标签:Codeforces1059C,GCD,sequence,int,Sequence,好题,remove,序列,include
From: https://blog.51cto.com/u_14932227/6042469

相关文章

  • POJ2487 Farey Sequence 欧拉函数模板题
    FareySequenceDescriptionTheFareySequenceFnforanyintegernwithn>=2isthesetofirreduciblerationalnumbersa/bwith0<a<b<=nandgcd(a,b)=1......
  • 2019-ACM-ICPC 徐州网络赛 M. Longest subsequence 序列自动机
    Stringisaveryusefulthingandasubsequenceofthesamestringisequallyimportant.Nowyouhaveastring ss withlength nn andastring tt withlen......
  • 2019南昌大学邀请赛 M. Subsequence 序列自动机
     Giveastring SS and NN string T_iTi ,determinewhether T_iTi isasubsequenceof SS.Iftiissubsequenceof SS,print ​​YES​​​,elseprint ......
  • 51NOD 1278 相离的圆(二分 + 排序 好题)
    平面上有N个圆,他们的圆心都在X轴上,给出所有圆的圆心和半径,求有多少对圆是相离的。例如:4个圆分别位于1,2,3,4的位置,半径分别为1,1,2,1,那么{1,2},{1,3}{2,3}{2,4......
  • 51nod 1138 连续整数的和 好题
    给出一个正整数N,将N写为若干个连续数字和的形式(长度>=2)。例如N=15,可以写为1+2+3+4+5,也可以写为4+5+6,或7+8。如果不能写为若干个连续整数的和,则输出NoS......
  • PostgreSQL数据库-Sequence的作用和用法
    PostgreSQL中的序列是一个数据库对象,本质上是一个自增器。所以,Sequence也可以通过在每个属性后加上autoincrment的值的形式存在。sequence的作用有两个方面:作为表的唯一......
  • 超级无敌神仙炫酷无敌原神大王好题。
    都是神题,难度3000上下。有些都是看题解做的,就当涨知识见世面了。学OI没做这些题,简直就是打游戏不玩原神,看vtb不看東雪蓮,听歌不听曹万江,成功学不学cjx,看闲话不看韩神,只......
  • 差分约束好题
    1、MagicProblem-7176(hdu.edu.cn)思路:求的是区间总和,所以考虑和前缀和进行结合,将前缀和a[i](前i个数的前缀和)作为边权。然后考虑限制条件。首先,区间[l,r]的总和小于......
  • Sequence Decoding (DFS)
    给出一个字符串,只含【,】,H,P,和数字,要把这些数字和中括号里的字母展开复制多少遍根据中括号外面的数字决定。用深搜来做,遇到数字就记录数字多少,遇到【就进深一度的搜索,如果数H......
  • [LeetCode]Permutation Sequence
    QuestionTheset[1,2,3,…,n]containsatotalofn!uniquepermutations.Bylistingandlabelingallofthepermutationsinorder,Wegetthefollowingsequen......