题意:
Jamie is preparing a Codeforces round. He has got an idea for a problem, but does not know how to solve it. Help him write a solution to the following problem:
Find k integers such that the sum of two to the power of each number equals to the number n and the largest integer in the answer is as small as possible. As there may be multiple answers, you are asked to output the lexicographically largest one.
To be more clear, consider all integer sequence with length k (a1, a2, ..., ak) with
. Give a value
to each sequence. Among all sequence(s) that have the minimum y value, output the one that is the lexicographically largest.
For definitions of powers and lexicographical order see notes.
The first line consists of two integers n and k (1 ≤ n ≤ 1018, 1 ≤ k ≤ 105) — the required sum and the length of the sequence.
Output "No" (without quotes) in a single line if there does not exist such sequence. Otherwise, output "Yes" (without quotes) in the first line, and k numbers separated by space in the second line — the required sequence.
It is guaranteed that the integers in the answer sequence fit the range [ - 1018, 1018].
23 5
Yes 3 3 2 1 0
13 2
No
1 2
Yes -1 -1
题解:
先求出N的二进制,二进制的1所在的位即所求幂次,如果1的个数大于K,则肯定不行;如果小于K,则进行增加操作,可将一个2^n变成两个2^(n-1)。注意:由于要求n的最大值最小,且字典序最大,增加位的时候从最高位开始,整体降位,即将所有的n全部变成n-1。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 64;
long long N, K;
int b[maxn*2];
int main()
{
scanf("%I64d%I64d", &N, &K);
for(int i = 0; i < maxn; i++){
K -= (b[maxn + i] = N >> i & 1);
}
if(K < 0) return puts("No") & 0;
for(int i = 2 * maxn - 1; i >= 1; i--){
if(b[i] > K) break;
K -= b[i];
b[i - 1] += b[i] << 1;
b[i] = 0;
}
int k = 0;
while(!b[k]) k++; b[k]--;
puts("Yes");
for(int i = 2 * maxn - 1; i >= k; i--){
while(b[i]--) printf("%d ", i - maxn);
}
for(int i = 1; i <= K; i++){
printf("%d ", k - i - maxn);
}
printf("%d\n", k - K - maxn);
return 0;
}
本文介绍了一道Codeforces竞赛题目B.Jamie and Binary Sequence的解决思路及实现代码。任务要求找到k个整数,使得这些整数的2的幂次之和等于给定的数n,并且最大的整数尽可能小,同时要求输出的序列字典序最大。
1312

被折叠的 条评论
为什么被折叠?



