Codeforces Round 474 C. Subsequence Counting

Subsequence Counting

time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

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 2n1 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_subsequenceMinimum_element_of_subsequenced

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} 1ai1018).

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_subsequenceMinimum_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_subsequenceMinimum_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 2n1 个子序列,所以我们只需要让题目中的 x x x 拆分成若干个 2 n i − 1 2^{n_i} - 1 2ni1​ 的次方的和即可

我们可以对 x x x 的二进制表示着手,从最高位遍历所有的 2 i − 1 2^i - 1 2i1,如果此时 x ≤ 2 i − 1 x \leq 2 ^ i - 1 x2i1,那么就从中取一个 2 i − 1 2^i - 1 2i1,对于每一个 2 i − 1 2^i - 1 2i1,当前元素最多出现两次(因为 3 × ( 2 i − 1 ) ≥ 2 i + 1 − 1 3\times(2^i-1)\geq 2^{i+1}-1 3×(2i1)2i+11),且下一个元素与当前元素相差 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值