codeforces 437B. The Child and Set 解题报告
题目链接:http://codeforces.com/contest/437/problem/B
题目意思:给出两个整数 sum 和 limit,问能否从1 ~ limit 这些数中选出一些数(注意:这些数最多只能取一次!),使得它们的lowbit 之和 等于 sum。不能则输出 -1。
这题总的来说比较直接。一开始犯了个比较傻的错误,以为除了2的幂以外,其他的 lowbit 都是1。还有一点需要注意的是,代码中 if (sum - t >= 0) 很重要!因为偶数的lowbit参差不齐,有可能sum-t 变为负数的!总后往前贪(limit ----> 1),能保证每次贪的数是最大的!这样也能尽快结束循环,防止超时。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std; const int maxn = 1e5 + ;
int ans[maxn]; int main()
{
int sum, limit;
while (cin >> sum >> limit)
{
memset(ans, , sizeof(ans));
int t, cnt = , tot = ;
for (int i = limit; i >= ; i--) // 从大到小开始贪心
{
if (i % == )
{
int count = ;
for (int j = i; !(j & ); j >>= ) // 从右往左看,1第一次出现的位置
count++;
t = pow(2.0, count); // 求出该偶数的lowbit
if (sum - t >= ) // 该数的lowbit能组成sum的条件
{
sum -= t;
ans[i] = ;
tot++;
}
}
else
{
sum--; // 奇数的lowbit是1
ans[i] = ;
tot++;
}
if (!sum)
break;
}
if (sum)
printf("-1\n");
else
{
printf("%d\n", tot);
for (int i = ; i <= limit; i++)
{
if (ans[i])
printf("%d ", i);
}
printf("\n");
}
}
return ;
}
codeforces 437B. The Child and Set 解题报告的更多相关文章
- codeforces 437A. The Child and Homework 解题报告
题目链接:http://codeforces.com/problemset/problem/437/A 题目意思:给出四个选项A.B.C.D选项的内容描述,要求选出符合以下条件的一项. (1)如果某个 ...
- Codeforces 437B The Child and Set
题目链接:Codeforces 437B The Child and Set 開始是想到了这样的情况,比方lowbit之后从大到小排序后有这么几个数,200.100,60.50.S = 210.那先选 ...
- codeforces C1. The Great Julya Calendar 解题报告
题目链接:http://codeforces.com/problemset/problem/331/C1 这是第一次参加codeforces比赛(ABBYY Cup 3.0 - Finals (onl ...
- codeforces B. Eugeny and Play List 解题报告
题目链接:http://codeforces.com/problemset/problem/302/B 题目意思:给出两个整数n和m,接下来n行给出n首歌分别的奏唱时间和听的次数,紧跟着给出m个时刻, ...
- codeforces 433C. Ryouko's Memory Note 解题报告
题目链接:http://codeforces.com/problemset/problem/433/C 题目意思:一本书有 n 页,每页的编号依次从 1 到 n 编排.如果从页 x 翻到页 y,那么| ...
- codeforces 556B. Case of Fake Numbers 解题报告
题目链接:http://codeforces.com/problemset/problem/556/B 题目意思:给出 n 个齿轮,每个齿轮有 n 个 teeth,逆时针排列,编号为0 ~ n-1.每 ...
- codeforces 510B. Fox And Two Dots 解题报告
题目链接:http://codeforces.com/problemset/problem/510/B 题目意思:给出 n 行 m 列只有大写字母组成的字符串.问具有相同字母的能否组成一个环. 很容易 ...
- codeforces 505A. Mr. Kitayuta's Gift 解题报告
题目链接:http://codeforces.com/problemset/problem/505/A 题目意思:给出一个长度不大于10的小写英文字符串 s,问是否能通过在字符串的某个位置插入一个字母 ...
- codeforces 499A.Inna and Pink Pony 解题报告
题目链接:http://codeforces.com/problemset/problem/499/A 题目意思:有两种按钮:1.如果当前观看的时间是 t,player 可以自动处理下一分钟,姑且理解 ...
随机推荐
- [NOIP2009] 提高组 洛谷P1072 Hankson 的趣味题
题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫 Hankson.现 在,刚刚放学回家的 Hankson 正在思考一个有趣的问题. 今天在课堂上,老师讲 ...
- 在 VirtualBox 5.0 系列中让虚拟机支持 USB 3.0 必须开启 APIC
VirtualBox 5.0 系列正式支持 USB 3.0,能够在宿主机支持 USB 3.0 的情况下,让虚拟机也选择具备 USB 3.0 的功能.但是经过多方试验,发现必须在 VirtualBox ...
- HDU4850 构造一个长度为n的串,要求任意长度为4的子串不相同
n<=50W.(使用26个字母) 构造方法:26个,最多构造出26^4种不同的串,长度最长是26^4+3,大于是输出"impossble",用四维数组判重.每次向前构造一位( ...
- InnoDB: The Auto-extending innodb_system data file './ibdata1' is of a different size 640 pages (rounded down to MB) than specified in the .cnf file: initial 768 pages, max 0 (relevant if non-zero) pa
2016-09-14T09:17:37.713955Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleane ...
- Spring中Bean的定义继承
以下内容引用自http://wiki.jikexueyuan.com/project/spring/bean-definition-inheritance.html: Bean定义继承 bean定义可 ...
- crontab 实际的应用
每二天执行一次: 0 0 */2 * * command #注意分,时不能为星*,否则每分钟执行 每天零晨01,03执行: 0 01,03 * * * command 每2小时执行一次 0 */2 * ...
- dwarf调试信息格式入门
https://www.prevanders.net/dwarf.html#testingcomment http://www.dwarfstd.org/ http://www.cnblogs.com ...
- 在matlab中对中国地图中的不同省份按照高度进行渲染
直接上优化后的代码和效果图 colour_totall=128; % 载入地图数据 --各省的多边形数据 shp_data=shaperead('maps/bou2_4p.shp', 'UseGeoC ...
- Go -- 并发编程的两种限速方法
引子 golang提供了goroutine快速实现并发编程,在实际环境中,如果goroutine中的代码要消耗大量资源时(CPU.内存.带宽等),我们就需要对程序限速,以防止goroutine将资源耗 ...
- Oracle 12c agent install for linux
安装Agent代理 在EM11g时,agent安装是通过在被监制主机端下载agent代理并安装,在EM12c版本号上,能够在EM12c服务端.通过"推送"的方式把agent代理在远 ...