Subsequence Counting
Pikachu had an array with him. He wrote down all the non-empty subsequences of the array on paper. Note that an array of size n has 2 n − 1 2^n-1 2n−1 non-empty subsequences in it.
Pikachu being mischievous as he always is, removed all the subsequences in which M a x i m u m _ e l e m e n t _ o f _ t h e _ s u b s e q u e n c e − M i n i m u m _ e l e m e n t _ o f _ s u b s e q u e n c e ≥ d \text Maximum\_element\_of\_the\_subsequence - Minimum\_element\_of\_subsequence \geq d Maximum_element_of_the_subsequence− Minimum_element_of_subsequence≥ d
Pikachu was finally left with X \text X X subsequences.
However, he lost the initial array he had, and now is in serious trouble. He still remembers the numbers X \text X X and d d d. He now wants you to construct any such array which will satisfy the above conditions. All the numbers in the final array should be positive integers less than 1 0 18 10^{18} 1018.
Note the number of elements in the output array should not be more than 1 0 4 10^4 104. If no answer is possible, print − 1 -1 −1.
Input
The only line of input consists of two space separated integers X \text X X and d d d ( 1 ≤ 1 \leq 1≤ X \text X X, d d d ≤ \leq ≤ 1 0 9 10^9 109).
Output
Output should consist of two lines.
First line should contain a single integer n ( 1 ≤ 1 \leq 1≤ n n n $\leq$10000)— the number of integers in the final array.
Second line should consist of n n n space separated integers — a 1 , a 2 , a 3 , … , a n a_1,a_2,a_3,\dots,a_n a1,a2,a3,…,an ( 1 ≤ a i ≤ 1 0 18 1\leq a_i \leq 10 ^{18} 1≤ai≤1018).
If there is no answer, print a single integer − 1 -1 −1. If there are multiple answers, print any of them.
Example
input
10 5
output
6
5 50 7 15 6 100
Note
In the output of the first example case, the remaining subsequences after removing those with M a x i m u m _ e l e m e n t _ o f _ t h e _ s u b s e q u e n c e − M i n i m u m _ e l e m e n t _ o f _ s u b s e q u e n c e ≥ 5 \text Maximum\_element\_of\_the\_subsequence - Minimum\_element\_of\_subsequence \geq 5 Maximum_element_of_the_subsequence− Minimum_element_of_subsequence≥ 5 are [ 5 ] , [ 5 , 7 ] , [ 5 , 6 ] , [ 5 , 7 , 6 ] , [ 50 ] , [ 7 ] , [ 7 , 6 ] , [ 15 ] , [ 6 ] , [ 100 ] [5], [5, 7], [5, 6], [5, 7, 6], [50], [7], [7, 6], [15], [6], [100] [5], [5, 7], [5, 6], [5, 7, 6], [50], [7], [7, 6], [15], [6], [100]. There are 10 10 10 of them. Hence, the array [ 5 , 50 , 7 , 15 , 6 , 100 ] [5, 50, 7, 15, 6, 100] [5, 50, 7, 15, 6, 100] is valid.
Similarly, in the output of the second example case, the remaining sub-sequences after removing those with M a x i m u m _ e l e m e n t _ o f _ t h e _ s u b s e q u e n c e − M i n i m u m _ e l e m e n t _ o f _ s u b s e q u e n c e ≥ 2 \text Maximum\_element\_of\_the\_subsequence - Minimum\_element\_of\_subsequence \geq 2 Maximum_element_of_the_subsequence− Minimum_element_of_subsequence≥ 2 are [ 10 ] , [ 100 ] , [ 1000 ] , [ 10000 ] [10], [100], [1000], [10000] [10], [100], [1000], [10000]. There are 4 of them. Hence, the array [ 10 , 100 , 1000 , 10000 ] [10, 100, 1000, 10000] [10, 100, 1000, 10000] is valid.
Tutorial
如果我们有 n n n 个元素,想要满足条件的子序列最多,那么让这 n n n 个元素都相等即可,此时可以得到 2 n − 1 2^n - 1 2n−1 个子序列,所以我们只需要让题目中的 x x x 拆分成若干个 2 n i − 1 2^{n_i} - 1 2ni−1 的次方的和即可
我们可以对 x x x 的二进制表示着手,从最高位遍历所有的 2 i − 1 2^i - 1 2i−1,如果此时 x ≤ 2 i − 1 x \leq 2 ^ i - 1 x≤2i−1,那么就从中取一个 2 i − 1 2^i - 1 2i−1,对于每一个 2 i − 1 2^i - 1 2i−1,当前元素最多出现两次(因为 3 × ( 2 i − 1 ) ≥ 2 i + 1 − 1 3\times(2^i-1)\geq 2^{i+1}-1 3×(2i−1)≥2i+1−1),且下一个元素与当前元素相差 d d d
此解法时间复杂度为 O ( log 2 x ) \mathcal O(\log^2x) O(log2x)
Solution
#include <bits/stdc++.h>
using namespace std;
int main(){
long long x, d, num = 1;
cin >> x >> d;
vector<long long> ans;
for (int i = 60; i; --i) {
long long cnt = (1ll << i) - 1;
while (x >= cnt) {
for (int j = 0; j < i; ++j) {
ans.emplace_back(num);
}
num += d;
x -= cnt;
}
}
cout << ans.size() << endl;
for (long long ai : ans) {
cout << ai << " ";
}
return 0;
}