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. xcode 删除 Provisioning Profile

    provisioning profile path: ~/Library/MobileDevice/Provisioning Profiles 打开并日期排序,删除老的 provisioning pr ...

  2. Fiddler 网页采集抓包利器

    最近这段时间,网页采集方面的工作做得比较多.用curl技术开发了一个微信文章聚合类产品,把抓取到的数据转换成json格式,并在android端调用json数据接口加以显示:基于weiphp做了一个掌上 ...

  3. Js参数RSA加密传输,jsencrypt.js的使用

    注意几点: 1.参数传递的+号处理,在传输时会把+变成空格,不处理后端就报错了. 1.前段代码 <!DOCTYPE html> <html> <head> < ...

  4. oracle单行函数之通用函数

    NVL (a,b) --当a=null时,返回b,否则返回a NVL2 (a, b, c) -- 当a=null时,返回c,否则返回b NULLIF (expr1, expr2) --当a=b时,返回 ...

  5. GUI对话框

    消息对话框 public static void showMessageDialog(Component parentComponent,String message,String title,int ...

  6. java——JNI(例子控制台(64位)清屏

    因为java的最底层是jvm,所以单纯的控制台java程序不能感知jvm再下面的操作系统的情况, 可以通过JNI(Java Native Interface)技术实现java后台调用C++/C的dll ...

  7. 记一次 nginx 504 Gateway Time-out

    今天程序在执行一项excel导出任务的时候 出现了nginx超时的提示 nginx 504 Gateway Time-out 排查过程: 查看该任务 发现内容是一个数据量20000条信息 每条信息有5 ...

  8. RSA 加密

    iOS开发教程-iOS中的RSA加解密 在移动应用开发中常常遇到数据传输安全性的问题,尤其是在账户安全以及支付场景中的订单数据.或支付信息的传输中,正规的公司一定会要求对数据进行加密,当然有创业初期的 ...

  9. IOS文件操作的两种方式:NSFileManager操作和流操作

    1.常见的NSFileManager文件方法 -(NSData *)contentsAtPath:path //从一个文件读取数据 -(BOOL)createFileAtPath: path cont ...

  10. 8.2.1.10 Nested-Loop Join Algorithms 嵌套循环 关联算法:

    8.2.1.10 Nested-Loop Join Algorithms 嵌套循环 关联算法: MySQL 执行关联在表之间使用一个嵌套循环算法或者变种 Nested-Loop Join Algori ...