time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.

Fortunately, Picks remembers something about his set S:

  • its elements were distinct integers from 1 to limit;
  • the value of  was
    equal to sum; here lowbit(x) equals 2k where k is
    the position of the first one in the binary representation of x. For example, lowbit(100102) = 102, lowbit(100012) = 12, lowbit(100002) = 100002 (binary
    representation).

Can you help Picks and find any set S, that satisfies all the above conditions?

在儿童节这一天,我们的小朋友来到了Picks的家,把他家里弄得一团糟。Picks非常生气。非常多重要的东西都不见了,这当中包含了Picks最喜欢的集合。

幸运的是,Picks记得一些关于他的集合S的事情:

1. 其元素是1至limit间的互异整数;

2. 全部lowbit(x)之和(x取遍S中的全部元素)等于sum,这里lowbit(x)等于2^k,k是x的二进制表示中第一个1的位置。比如(下面数字均为二进制表示),lowbit(10010)=10,lowbit(10001)=1,lowbit(10000)=10000。

你能帮助Picks找到一个符合上述条件的集合S吗?

Input

The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 105). 一行,两个整数,依次是sum和limit(1<=sum,limit<=10^5)。

Output

In the first line print an integer n (1 ≤ n ≤ 105),
denoting the size of S. Then print the elements of set S in
any order. If there are multiple answers, print any of them.

If it's impossible to find a suitable set, print -1.

第一行输出n(1<=n<=10^5),为集合S的大小。然后在下一行以随意顺序输出S的全部元素。假设有多个符合要求的集合,输出随意一个就可以。

假设找不到这种集合,输出-1。

Sample test(s)

input
5 5
output
2
4 5
input
4 3
output
3
2 3 1
input
5 1
output
-1
Note

In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.

In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4.

题解

lowbit就是x&(-x)。其余为模拟。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int m,l,ans[100002];
struct shu {int w,b;} a[100002];
int lowbit(int x) {return x&(-x);}
bool kp(const shu &x,const shu &y)
{return x.b>y.b;}
void doit()
{
sort(a+1,a+l+1,kp);
for(int i=1;i<=l;i++)
{if(m>=a[i].b)
{m-=a[i].b;
ans[++ans[0]]=a[i].w;
}
}
if(m)printf("-1");
else
{
printf("%d\n",ans[0]);
for(int i=1;i<=ans[0];i++)
printf("%d ",ans[i]);
}
}
int main()
{
scanf("%d%d",&m,&l);
for(int i=1;i<=l;i++)
{a[i].w=i; a[i].b=lowbit(i);}
doit();
return 0;
}

codeforce 437B The Child and Set的更多相关文章

  1. Codeforces 437B The Child and Set

    题目链接:Codeforces 437B The Child and Set 開始是想到了这样的情况,比方lowbit之后从大到小排序后有这么几个数,200.100,60.50.S = 210.那先选 ...

  2. codeforces 437B. The Child and Set 解题报告

    题目链接:http://codeforces.com/contest/437/problem/B 题目意思:给出两个整数 sum 和 limit,问能否从1 - limit 这些数中选出一些数(注意: ...

  3. 【奶昔队ROUND#1】

    奶昔队Round #1 热身 (奶昔队不是真正的队,是群) CodeForces 435C Cardiogram 模拟,不过我做的时候不是模拟,是计算...(写了好久,还wa了几次),现在看了别人的代 ...

  4. Codeforce 438D-The Child and Sequence 分类: Brush Mode 2014-10-06 20:20 102人阅读 评论(0) 收藏

    D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input st ...

  5. Codeforce 水题报告(2)

    又水了一发Codeforce ,这次继续发发题解顺便给自己PKUSC攒攒人品吧 CodeForces 438C:The Child and Polygon: 描述:给出一个多边形,求三角剖分的方案数( ...

  6. MapReduce剖析笔记之七:Child子进程处理Map和Reduce任务的主要流程

    在上一节我们分析了TaskTracker如何对JobTracker分配过来的任务进行初始化,并创建各类JVM启动所需的信息,最终创建JVM的整个过程,本节我们继续来看,JVM启动后,执行的是Child ...

  7. [翻译]AKKA笔记 - CHILD ACTORS与ACTORPATH -6

    原文:http://rerun.me/2014/10/21/akka-notes-child-actors-and-path/ Actor是完全的继承结构.你创建的任何Actor肯定都是一个其他Act ...

  8. php php-5.6.4.tar.bz2 apache 兼容问题 child pid 27858 exit signal Segmentation fault

    环境 [root envirotar]# uname -a Linux i2..el6.x86_64 # SMP Thu Jul :: UTC x86_64 x86_64 x86_64 GNU/Lin ...

  9. [ASP.NET MVC 小牛之路]12 - Section、Partial View 和 Child Action

    概括的讲,View中的内容可以分为静态和动态两部分.静态内容一般是html元素,而动态内容指的是在应用程序运行的时候动态创建的内容.给View添加动态内容的方式可归纳为下面几种: Inline cod ...

随机推荐

  1. sql数据库之间数据的转录

    private void Form1_Load(object sender, EventArgs e) { BindDataBase(combDataBaseNew, , ""); ...

  2. 制作PHP安装程序的原理和步骤56

    制作PHP安装程序的原理和步骤56 1.制作PHP安装程序的原理和步骤检查目录或文件的权限----修改或填加配置文件---检查配置文件正 确性---导入数据库----锁定或删除安装文件 原理: 其实P ...

  3. ASP.NET MVC下的异步Action的定义和执行原理

    一.基于线程池的请求处理ASP.NET通过线程池的机制处理并发的HTTP请求.一个Web应用内部维护着一个线程池,当探测到抵达的针对本应用的请求时,会从池中获取一个空闲的线程来处理该请求.当处理完毕, ...

  4. Python入门100例题

    原文链接:http://www.cnblogs.com/CheeseZH/archive/2012/11/05/2755107.html 无论学习哪门计算机语言,只要把100例中绝大部分题目都做一遍, ...

  5. shape及其子节点详解

    shape最大的最用便是用来替代图片,释放磁盘空间.另外则是增加适应不通过屏幕的设备. 先来看看shape下面的节点以及它所起到的作用. gradient   -- 对应颜色渐变(startcolor ...

  6. iOS推送 再备

    这是一篇编译的文章,内容均出自Parse.com的iOS开发教程,同时作者还提供了视频讲解.本文将带领开发者一步一步向着iOS推送通知的深处探寻,掌握如何配置iOS推送通知的奥义. 介绍一点点背景资料 ...

  7. 使用hexo创建github博客

    前言 前面说过,之前用wordpress辛辛苦苦搭建的博客,因为服务器和域名的问题挂掉了.后来发现github也能够搭建自己的博客,不需要去关心主机域名的问题,而且还能使用Markdown来写博客,就 ...

  8. ionic ng-src 在网页显示,但是导出apk在android手机中运行不显示图片

    解决方法参照: http://stackoverflow.com/questions/29896158/load-image-using-ng-src-in-android-ionic-aplicat ...

  9. Ubuntu 下启动/停止/重启mysql服务

    1:sudo start mysql 2:sudo stop mysql 3:sudo restart mysql

  10. Ubuntu 下对ADT 添加别名(alias)

    1:~$ vim .bashrc 2:在打开的.bashrc文件中加入: alias adt='./adt-bundle-linux-x86-20130729/eclipse/eclipse' 3:保 ...