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 可以自动处理下一分钟,姑且理解 ...
随机推荐
- 洛谷P2365 任务安排 [解法一]
题目描述 N个任务排成一个序列在一台机器上等待完成(顺序不得改变),这N个任务被分成若干批,每批包含相邻的若干任务.从时刻0开始,这些任务被分批加工,第i个任务单独完成所需的时间是Ti.在每批任务开始 ...
- OI 数论整理
1.素数: 质数(prime number)又称素数,有无限个.一个大于1的自然数,除了1和它本身外,不能被其他自然数整除,换句话说就是该数除了1和它本身以外不再有其他的因数;否则称为合数. 2016 ...
- 【HDOJ5949】Relative atomic mass(签到)
题意:给定一个只由H.C.O三种分子组成物质的分子式,求相对分子质量 len<=10 思路:队友写的 #include <stdio.h> #include <vector&g ...
- 【Codevs1907】方格取数3(最小割)
题意:在一个有m*n 个方格的棋盘中,每个方格中有一个正整数.现要从方格中取数,使任意2 个数所在方格没有公共边,且取出的数的总和最大.试设计一个满足要求的取数算法. n,m<=30 思路:如果 ...
- HDU 4474 Yet Another Multiple Problem【2012成都regional K题】 【BFS+一个判断技巧】
Yet Another Multiple Problem Time Limit: 40000/20000 MS (Java/Others) Memory Limit: 65536/65536 K ...
- vue搭建cli脚手架环境(出现问题及解决,主要是node版本低)
Vue 提供了一个官方的cli,为单页面应用 (SPA) 快速搭建繁杂的脚手架. 一.vue cli脚手架 脚手架通过webpack搭建开发环境 使用ES6语法 打包压缩js为一个文件 项目文件在环境 ...
- T1008 选数 codevs
http://codevs.cn/problem/1008/ 题目描述 Description 已知 n 个整数 x1,x2,…,xn,以及一个整数 k(k<n).从 n 个整数中任选 k 个整 ...
- File类 文件过滤器
创建过滤器 package cn.zmh.File; import java.io.File; import java.io.FileFilter; // 实现FileFilter类的功能 publi ...
- java学习笔记(四)面向对象
一.形參长度可变的方法 当传入被调用的函数參数数量不确定时,在方法最后一个形參的类型后加上三个点号(...),表明该形參能够接受多个參数值.多个參数值被当做数组传入,这些參数必须为指定的类型. pac ...
- CS 和 BS 的区别和优缺点(转)
转自:http://www.cnblogs.com/scnuyz/p/5808808.html bs是浏览器(browser)和服务器(server) cs是静态客户端程序(client)和服务器(s ...