codeforce 437B The Child and Set
1 second
256 megabytes
standard input
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)
5 5
2
4 5
4 3
3
2 3 1
5 1
-1
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的更多相关文章
- Codeforces 437B The Child and Set
题目链接:Codeforces 437B The Child and Set 開始是想到了这样的情况,比方lowbit之后从大到小排序后有这么几个数,200.100,60.50.S = 210.那先选 ...
- codeforces 437B. The Child and Set 解题报告
题目链接:http://codeforces.com/contest/437/problem/B 题目意思:给出两个整数 sum 和 limit,问能否从1 - limit 这些数中选出一些数(注意: ...
- 【奶昔队ROUND#1】
奶昔队Round #1 热身 (奶昔队不是真正的队,是群) CodeForces 435C Cardiogram 模拟,不过我做的时候不是模拟,是计算...(写了好久,还wa了几次),现在看了别人的代 ...
- 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 ...
- Codeforce 水题报告(2)
又水了一发Codeforce ,这次继续发发题解顺便给自己PKUSC攒攒人品吧 CodeForces 438C:The Child and Polygon: 描述:给出一个多边形,求三角剖分的方案数( ...
- MapReduce剖析笔记之七:Child子进程处理Map和Reduce任务的主要流程
在上一节我们分析了TaskTracker如何对JobTracker分配过来的任务进行初始化,并创建各类JVM启动所需的信息,最终创建JVM的整个过程,本节我们继续来看,JVM启动后,执行的是Child ...
- [翻译]AKKA笔记 - CHILD ACTORS与ACTORPATH -6
原文:http://rerun.me/2014/10/21/akka-notes-child-actors-and-path/ Actor是完全的继承结构.你创建的任何Actor肯定都是一个其他Act ...
- 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 ...
- [ASP.NET MVC 小牛之路]12 - Section、Partial View 和 Child Action
概括的讲,View中的内容可以分为静态和动态两部分.静态内容一般是html元素,而动态内容指的是在应用程序运行的时候动态创建的内容.给View添加动态内容的方式可归纳为下面几种: Inline cod ...
随机推荐
- Android开发手记(11) 滑动条SeekBar
安卓滑动条的操作特别简单,通过getProgress()可以获得SeekBar的位置,通过setProgress(int progress)可以设置SeekBar的位置.要想动态获取用户对SeekBa ...
- Cacti监控Linux主机
1 要监视一台Linux主机,需要在被监控的主机上安装net-snmp相关软件包,CentOS安装可使用“yum -y install net-snmp”命令:# yum -y install net ...
- iOS 网络与多线程--8.百度地图的使用(调用系统浏览器)
通过调用设备自带的浏览器,打开百度地图 // 1.定义一个方法,用来打开谷歌地图的功能 -(IBAction)openMaps { // 2.定义一个字符串,作为百度地图的当前地理位置 废弃 NSSt ...
- Swift中可选型的Optional Chaining 和 Nil-Coalesce(Swift2.1)
/* 下面是介绍Optional Chaining 和 Nil-Coalesce */ // Optional Chaining (可选链) if let errorMessage = errorMe ...
- PHP Zip File
安装 如需在服务器上运行 Zip File 函数,必须安装这些库: Guido Draheim 的 ZZIPlib 库: 下载 ZZIPlib 库 Zip PELC 扩展:下载 Zip PELC 扩展 ...
- Css Div半透明
用CSS控制外层DIV不透明,而内层DIV透明,这样实现的效果是意想不到的,还不错吧,其实代码也是很简单的,也很好理解,主要是用了CSS的滤镜. <html xmlns="http:/ ...
- java Byte.toString 方法与String.ValueOf(Byte)效率比较
int times = 10000000; Byte[] li = new Byte[times]; for (int i = 0; i < times; i++) { li[i] = (byt ...
- java 使用正则表达式对文件名非法字符处理
1.文件名在操作系统中不允许出现 / \ " : | * ? < > 故将其以空替代 Pattern pattern = Pattern.compile(" ...
- 微信 回复多图文 借助php框架
private function replyMostPhoto($data,$arr){$this->logger("已经到达回复多图文!".$arr[0]['Title'] ...
- php empty和isset的区别
通过一个例子来解释一下, $id = 0 empty($id) 的值是true isset($id) 也是true. empty 和isset 处理对象无外乎 未定义常量 .0 .空字符串 如果变量 ...