<传送门>

【题目大意】

给你一个sum和一个limit,现在要你在1~limit中找到一些数来使得这些数的和等于sum,如果能找到的话就输出找到的数的个数和这些数,未找到输出"-1"。

比赛的时候被hack了。

【题目分析】

这题需要将所有的数的lowbit先求出来,然后按照大小排序,然后从后往前判断,如果这个数小于sum那么这个数就是可以构成sum的数,选进去,完了以后判断sum的值是否为0就可以了。做题的时候没将题目理解透彻。

#include<bits/stdc++.h>
#define MAX 100010
using namespace std;
struct Node
{
int num;
int id;
bool vis=;
};
Node node[MAX];
int sum,limit;
int lowbit(int x)
{
return x&(-x);
}
bool cmp(Node a,Node b)
{
return a.num<b.num;
}
int main()
{
scanf("%d%d",&sum,&limit);
int i,j;
for(i=;i<=limit;i++)
{
node[i].num=lowbit(i);
node[i].id=i;
}
sort(node+,node++limit,cmp);
int cnt=;
for(i=limit;i>=;i--)
{
if(node[i].num<=sum)
{
sum-=node[i].num;
node[i].vis=;
cnt++;
}
} if(sum)
printf("-1");
else
{
printf("%d\n",cnt);
for(i=limit;i>=;i--)
{
if(node[i].vis)
{
printf("%d ",node[i].id);
}
}
}
putchar();
return ;
}

codeforces --- Round #250 (Div. 2) B. The Child and Set的更多相关文章

  1. Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间取摸

    D. The Child and Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...

  2. Codeforces Round #250 (Div. 1) B. The Child and Zoo 并查集

    B. The Child and Zoo Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/438/ ...

  3. Codeforces Round #250 (Div. 1) A. The Child and Toy 水题

    A. The Child and Toy Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/438/ ...

  4. Codeforces Round #250 (Div. 1) D. The Child and Sequence (线段树)

    题目链接:http://codeforces.com/problemset/problem/438/D 给你n个数,m个操作,1操作是查询l到r之间的和,2操作是将l到r之间大于等于x的数xor于x, ...

  5. Codeforces Round #250 (Div. 2)—A. The Child and Homework

         好题啊,被HACK了.曾经做题都是人数越来越多.这次比赛 PASS人数 从2000直掉 1000人  被HACK  1000多人! ! ! ! 没见过的科技啊 1 2 4 8 这组数 被黑的 ...

  6. Codeforces Round #250 (Div. 1) D. The Child and Sequence(线段树)

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

  7. Codeforces Round #250 (Div. 1) D. The Child and Sequence

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

  8. Codeforces Round #250 (Div. 2) D. The Child and Zoo 并查集

    D. The Child and Zoo time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  9. Codeforces Round #250 (Div. 2)B. The Child and Set 暴力

    B. The Child and Set   At the children's day, the child came to Picks's house, and messed his house ...

  10. Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间求和+点修改+区间取模

    D. The Child and Sequence   At the children's day, the child came to Picks's house, and messed his h ...

随机推荐

  1. python实现余弦相似度文本比较

    向量空间模型VSM: VSM的介绍: 一个文档可以由文档中的一系列关键词组成,而VSM则是用这些关键词的向量组成一篇文档,其中的每个分量代表词项在文档中的相对重要性. VSM的例子: 比如说,一个文档 ...

  2. Javascript总结(全)

    说明 个人感觉总结得很好忍不住要分享,转载自github地址,作者博客原文发现打不开,于是复制粘贴到此处,如有侵权,请联系本人删除. Type class TypeFn { isString (o) ...

  3. pycharm配置默认代码和注释模板

    有人问,在pycharm新建python文件时,文件开头的注释每次都要重复写,能不能配置成模板?其实pycharm本身自带此功能 在pycharm菜单栏找File -> settings -&g ...

  4. CentOS里下载和离线安装rpm包

    离线下载rpm包    yum --downloadonly --downloaddir=/home/packages install mariadb-devel 安装离线rpm包    进入下载包的 ...

  5. kafka(一)设计分析

    参考文档:Kafka 设计与原理详解:http://blog.csdn.net/suifeng3051/article/details/48053965Kafka深度解析:http://blog.cs ...

  6. SpringCache

    官方文档:https://docs.spring.io/spring/docs/4.3.13.RELEASE/spring-framework-reference/htmlsingle/#cache ...

  7. unity EditorGUILayer绘制报错

    最近在开发一个可视化工具的时候,遇到了一个代码错误,小小的记录一下 具体的报错信息:ArgumentException: Getting control 0's position in a group ...

  8. 解决'maven lifecycle mapping not converted'的问题

    基于maven的项目,使用各种maven plugin来完成开发中的各种工作,例如编译代码,打包,部署等等… 每个plugin包含许多的goal,用来做特定的事情.典型的基于java的maven项目就 ...

  9. qt5 源码编译

    源码 qt-everywhere-src-5.11.3 依赖 apt-get install bison build-essential gperf flex ruby python libasoun ...

  10. Spark2.x(六十一):在Spark2.4 Structured Streaming中Dataset是如何执行加载数据源的?

    本章主要讨论,在Spark2.4 Structured Streaming读取kafka数据源时,kafka的topic数据是如何被执行的过程进行分析. 以下边例子展开分析: SparkSession ...