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. if..endif 语法

    使用 if(); elseif(); else; endif; 这一系列复杂的语句无助于 PHP 3.0 解析器的效率.因此,语法改变为: Example#1 移植:旧有 if..endif 语法 i ...

  2. mysql存储过程controller的not found造成混乱的解决办法

    打草稿 问题:对于select  var1 into var2 ,单select的结果为空的时候(空的意思是没有找到数据,不是该值为null) ,会触发controller的not found 当游标 ...

  3. CSS 解决<td>里面内容太多把表格弄变形的原因,设置 自动换行。

    1 < style="word-break:break-all"> 例如div宽200px,它的内容就会到200px自动换行,如果该行末端有个英文单词很长(congra ...

  4. jquery中的replaceWith()和html()有什么区别?

    区别在于,html()会替换指定元素内部的HTML,而replaceWith()会替换元素本身及其内部的HTML. 例子: 1 <div id="myid" /> 1 ...

  5. jQuery get/post区别及contentType取值

    1.GET访问 浏览器 认为 是等幂的 就是 一个相同的URL 只有一个结果[相同是指 整个URL字符串完全匹配]所以 第二次访问的时候 如果 URL字符串没变化浏览器是直接拿出了第一次访问的结果,表 ...

  6. 无缝滚动js (手写通俗易懂)

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  7. 一个php小白找工作的历程

    一个php小白找工作的历程其实对新工作还是有点忐忑的,对于我这样一个有着特殊工作经历的来说更是如此.为了更好的迎接未来,不得不总结下过去.在经历一段时间的职业生涯探索期后,还是觉得自己更适合做程序员这 ...

  8. Centos下删除文件名乱码文件

    centos下通过rm命令来删除文件,但是如果要删除文件名乱码的文件,就不能直接使用rm命令了,因为压根就无法输出文件名来.不过借助find命令可以实现对其删除.在linux下对于每个文件都一个对应的 ...

  9. 基于python的接口测试学习笔记一(初出茅庐)

    第一次写博客笔记,讲一下近来学习的接口自动化测试.网上查阅了相关资料,最后决定使用python语言写接口测试,使用的是python的第三方库requests.虽然python本身标准库中的 urlli ...

  10. 机器学习之python: kNN

    ################################################## # kNN : k Nearest Neighbour # Author : Monne # Da ...