题目大意:给出sum和limit,用不超过limit的数,并且不重复的数组成lowbit和为sum的方案。lowbit(num)表示小于num的最大2k。
解题思路:首先暴力处理出1到limit的所有lowbit,然后排序,sum从最大的开始减掉。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5+5;
struct state {
int num;
int low;
}s[N];
int n, sum, cnt, ans[N];
inline bool cmp (const state&a, const state& b) {
return a.low > b.low;
}
int getLow (int x) {
int c = 0;
while (x) {
if (x&1)
return 1<<c;
x /= 2;
c++;
}
return 0;
}
void init () {
scanf("%d%d", &sum, &n);
for (int i = 1; i <= n; i++) {
s[i].num = i;
s[i].low = getLow(i);
}
cnt = 0;
sort(s + 1, s + n + 1, cmp);
}
int main () {
init ();
for (int i = 1; i <= n; i++) {
if (s[i].low <= sum) {
ans[cnt++] = s[i].num;
sum -= s[i].low;
}
}
if (sum)
printf("-1\n");
else {
printf("%d\n%d", cnt, ans[0]);
for (int i = 1; i < cnt; i++)
printf(" %d", ans[i]);
printf("\n");
}
return 0;
}
分享到:
相关推荐
Codeforces 1105B - Zuhair and Strings 测试点37个(全)
Codeforces - 1107B. Digital root & 1107C. Brutality(规律 & 贪心)Codeforces - 1107B.
Codeforces-Problem-Set
Codeforces 题库 101-200 共~500题 codeforces.com版权所有。 程序可提交至该网站评测。
Codeforces 题库 001-100 共~500题 codeforces.com版权所有。 程序可提交至该网站评测。
codeforces编程网站预测分数插件
使用于Google Chrome的Codeforces Enhancer 1.1.2插件安装包。 版本:codeforces enhancer 1.1.2 使用浏览器:Google Chrome
Some of the Codeforces problems codes
题意: 给出 nnn 个点,n−1n-1n−1 条边,最多询问 n2\frac{n}{2}2n 次,每次询问 u,vu,vu,v,会给出 uvuvuv的最近公共祖先,...set g[N]; int ans[N]; int main() { sd(n); rep(i, 1, n - 1) { sdd(u, v); g[u
题目大意:给出一个由 n 个点组成的树,现在可以询问 n/2 次(向下取整)LCA,确定根节点是哪个节点 题目分析:因为最多只能求 n/2 次lca,每次需要两个节点作为参数,也就是说每个点我们至多遍历一遍,读完题后没什么...
codeforces每日一题。 题意: 给出一个数组,让你挑选出能组成任意pair差值为2的幂的序列,并且使这个序列长度尽可能大。 思路: 针对于挑选出来的序列,任意pair对的差值为2的幂数。 假设有多个pair对,设dis(a,b)=2...
暴枚最长桌脚的长度$l$,然后长度比$l$长的桌脚全部都要砍掉长度比$l$短的桌脚选择代价前$k$小的砍掉用线段树维护;示例程序 :typedef long l
Codeforces 185A - Plant 全测试点49个
codeforces 19 E Fairy 一道比较难的题目的解题报告 推荐阅读
Codeforces global round 10 codes
Codeforces round 678 division 2 codes
codeforces每日一练。 题意: 给一棵树,每个点有一个点权,每条边有一个边权,求一条链使得点权和-边权和最大。 思路: 由于我没看清楚题意,以为是求联通子图的点权和-边权和最大,用link-cut-tree写换根,wa10了两...
lucifer1004大佬的博客cf上分攻略故里大佬的githubcf思维题刷题数:44- (1421)codeforces 676 div2 A,B done
Codeforces部门2,A # And a2oj Ladder 4 some problems Ladder URL:http://a2oj.com/Ladder.jsp?ID=4难度等级:2问题提示: 1- 4A....... http://codeforc
Codeforces round 678 D2_Codeforces_源码