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 non-empty subsequences in it. 

Pikachu being mischievous as he always is, removed all the subsequences in which Maximum_element_of_the_subsequence  -  Minimum_element_of_subsequence  ≥ d

Pikachu was finally left with X subsequences. 

However, he lost the initial array he had, and now is in serious trouble. He still remembers the numbers X and 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 10^18. 

Note the number of elements in the output array should not be more than 10^4. If no answer is possible, print  - 1.


-----Input-----

The only line of input consists of two space separated integers X and d (1 ≤ X, d ≤ 10^9).


-----Output-----

Output should consist of two lines.

First line should contain a single integer n (1 ≤ n ≤ 10 000)— the number of integers in the final array.

Second line should consist of n space separated integers — a_1, a_2, ... , a_{n} (1 ≤ a_{i} < 10^18).

If there is no answer, print a single integer -1. If there are multiple answers, print any of them.


-----Examples-----
Input
10 5

Output
6
5 50 7 15 6 100
Input
4 2

Output
4
10 100 1000 10000


-----Note-----

In the output of the first example case, the remaining subsequences after removing those with 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]. There are 10 of them. Hence, the array [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 Maximum_element_of_the_subsequence  -  Minimum_element_of_subsequence  ≥ 2 are [10], [100], [1000], [10000]. There are 4 of them. Hence, the array [10, 100, 1000, 10000] is valid.

Difficulty: MEDIUM_HARD
Tags: Bit manipulation

Constructive algorithms

Implementation

Greedy algorithms
Skill types: Bit manipulation

Greedy algorithms

Starter code:

Solutions:
p2 = [(i, 2 ** i - 1) for i in range(1, 40)]
(X, d) = map(int, input().split(' '))
lens = []
for i in p2[::-1]:
	if X >= i[1]:
		X -= i[1]
		lens.append(i[0])
el = 1
if sum(lens) >= 10000:
	print(-1)
	exit(0)
print(sum(lens) + X)
for i in lens:
	print((str(el) + ' ') * i, end='')
	el += d
for i in range(X):
	print(el, end=' ')
	el += d


(x, d) = map(int, input().split())
n = 1
ans = []
cnt = 0
for i in range(60, 0, -1):
	if x >> i & 1:
		for j in range(1, i + 1):
			ans.append(n)
		n += d
		cnt += 1
while cnt:
	ans.append(n)
	n += d
	cnt -= 1
if x & 1:
	ans.append(n)
print(len(ans))
for i in ans:
	print(i, end=' ')


def m2(n):
	r = 1
	d = 0
	while (r << 1) - 1 <= n:
		r <<= 1
		d += 1
	return (r - 1, d)
(x, d) = [int(x) for x in input().split()]
le = 0
ost = x
nums = []
kols = []
inf = 10 ** 18
last_num = 1 - d
while ost != 0:
	(n, deg) = m2(ost)
	ost -= n
	le += deg
	last_num += d
	nums.append(last_num)
	kols.append(deg)
	if le > 10 ** 4:
		print(-1)
		exit()
	if last_num > inf:
		print(-1)
		exit()
print(sum(kols))
for i in range(len(nums)):
	print(*[nums[i]] * kols[i], end=' ')


from math import log2
(x, d) = map(int, input().split())
n = 0
arr = []
v = 1
while x > 0:
	t = int(log2(x + 1))
	arr += [v] * t
	n += t
	x -= 2 ** t - 1
	v += d
print(n)
for i in arr:
	print(i, end=' ')


import sys
input = sys.stdin.buffer.readline
(x, d) = map(int, input().split())
bit = 0
count = 1
lst = []
total = 1
remind = 0
while x:
	count = 0
	bit = 0
	while bit <= x:
		bit = 1 << count
		count += 1
	bit = bit >> 1
	count -= 2
	if not lst:
		for i in range(count):
			lst.append(1)
		remind += 1
	else:
		item = lst[-1] + d
		for i in range(count):
			lst.append(item)
		remind += 1
	x -= bit
	total += 1
for i in range(remind):
	if lst:
		item = lst[-1] + d
	else:
		item = 1
	lst.append(item)
print(len(lst))
print(*lst)


from math import *
R = lambda : map(int, input().split())
(x, d) = R()
n = 0
arr = []
v = 1
while x:
	t = floor(log2(x + 1))
	arr += [v] * t
	n += t
	v += d
	x -= 2 ** t - 1
print(n)
print(' '.join(map(str, arr)))


(x, d) = map(int, input().split())
s = str(bin(x))[2:]
wqa = 1
ans = []
koms = 0
for i in range(len(s)):
	if s[i] == '1':
		for idq in range(len(s) - i - 1):
			ans.append(wqa)
		koms += 1
		wqa += d + 1
for ii in range(koms):
	ans.append(wqa)
	wqa += d
print(len(ans))
print(*ans)


(x, d) = list(map(int, input().split()))
arr = []
n = 0
s = ''
while x > 0:
	s += str(x % 2)
	x //= 2
f = 1
l = 999999999999999999
for i in range(len(s)):
	if int(s[i]):
		arr += [f] * i + [l]
		f += d
		l -= d
		n += i + 1
if n == -1:
	print(-1)
else:
	print(n)
	print(*arr)


(x, d) = map(int, input().split())
l = []
a = 1
while x >= 0:
	c = 1
	while 2 ** c - 1 <= x:
		c += 1
	c -= 1
	l = l + [a] * c
	a += d
	x -= 2 ** c - 1
	if c == 0:
		x -= 1
a -= d
if len(l) > 10 ** 4 or l[-1] > 10 ** 18:
	exit(print(-1))
print(len(l))
ans = ''
for i in l:
	ans += '{} '.format(i)
print(ans)


(x, d) = map(int, input().split())
start = 1
ans = []
for i in range(61):
	if x & 1 << i:
		for j in range(i):
			ans.append(start)
		start += d
		ans.append(start)
		start += d
print(len(ans))
print(' '.join(map(str, ans)))


from math import log
from math import floor

def main():
	(X, d) = map(int, input().split())
	n = 0
	l = []
	v = 1
	while X > 0:
		m = floor(log(X + 1, 2))
		l += [v] * m
		n += m
		v += d
		X -= 2 ** m - 1
	print(n)
	print(' '.join([str(x) for x in l]))
main()


(X, D) = map(int, input().split())
cn = 1
add0 = 1 if X & 1 else 0
ans = []
for i in range(30, 0, -1):
	if not X & 1 << i:
		continue
	ans += [cn] * i
	add0 += 1
	cn += D
for i in range(add0):
	ans.append(cn)
	cn += D
print(len(ans))
print(' '.join(map(str, ans)))


(x, d) = map(int, input().split())

def get_next(x):
	if bin(x + 1).count('1') == 1:
		return bin(x).count('1')
	return len(bin(x)) - 3
a = []
w = 1
while x:
	c = get_next(x)
	a += c * [w]
	w += d + 1
	x -= 2 ** c - 1
print(len(a))
print(*a)


(x, d) = map(int, input().split())
ans = []
cur = 1
while x > 0:
	k = 1
	while 2 ** k - 1 <= x:
		k += 1
	k -= 1
	for i in range(k):
		ans.append(cur)
	x -= 2 ** k - 1
	cur += d
print(len(ans))
print(*ans)


(x, d) = map(int, input().split())
pw = [2 ** i - 1 for i in range(31, -1, -1)]
p = [i for i in range(31, -1, -1)]
ans = []
el = 1
i = 0
while x > 0:
	while pw[i] > x:
		i += 1
	for z in range(p[i]):
		ans.append(el)
	x -= pw[i]
	el += d
if x != 0:
	print('-1')
else:
	print(len(ans))
	print(*ans)


from sys import stdin, stdout
(x, d) = map(int, stdin.readline().split())
psze = 100
cur = 1
ans = []
while x:
	for z in range(1, psze):
		v = (1 << z) - 1
		if v > x:
			i = z - 1
			x = x - (1 << i) + 1
			break
	for j in range(i):
		ans.append(cur)
	cur += d
stdout.write(str(len(ans)) + '\n' + ' '.join(list(map(str, ans))))


from math import *
from collections import *
import sys
sys.setrecursionlimit(10 ** 9)
l = [2 ** i - 1 for i in range(40)]
(x, d) = map(int, input().split())
a = []
ct = 1
while x != 0:
	for i in range(39):
		if l[i + 1] > x:
			ind = i
			break
	x -= l[ind]
	for i in range(ind):
		a.append(ct)
	ct += d
print(len(a))
for i in a:
	print(i, end=' ')


(x, d) = [int(i) for i in input().split()]
gr = []
a = [2 ** i - 1 for i in range(31)]
j = 30
while x > 0:
	while a[j] > x:
		j -= 1
	gr.append(j)
	x -= a[j]
n = sum(gr)
if n > 10 ** 4:
	print(-1)
	exit()
print(n)
cur = 1
for i in gr:
	for j in range(i):
		print(cur, end=' ')
	cur += d


(X, d) = map(int, input().split())
a = list()
cur = 1
for i in range(32):
	if X & 1 << i:
		for j in range(i):
			a.append(cur)
		cur = cur + d
		a.append(cur)
		cur = cur + d
print(len(a))
for i in a:
	print(i, end=' ')


import sys
import math
from queue import *
import random
input = sys.stdin.readline

def inp():
	return int(input())

def inara():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def bit(n):
	ans = -1
	while n:
		ans += 1
		n //= 2
	return ans
(x, d) = invr()
q = PriorityQueue()
ans = []
q.put(x)
while not q.empty():
	u = q.get()
	if u == 1:
		ans.append(u)
		continue
	ans.append(bit(u))
	q.put(u - 2 ** bit(u) + 1)
answer = []
last = 1
for num in ans:
	for i in range(num):
		answer.append(last)
	last += d
random.shuffle(answer)
print(len(answer))
print(*answer)


from math import log2, floor
from itertools import chain

def get_me_array(x, value):
	b = floor(log2(x))
	array = [value] * b
	return (array, x - (2 ** b - 1))

def solution(x, d):
	d += 1
	value = 1
	A = []
	while x > 5:
		(array, x) = get_me_array(x, value)
		value += d
		A.append(array)
	a = []
	for i in range(x):
		a.append(value + i * d)
	A.append(a)
	return list(chain.from_iterable(A))
(x, d) = map(int, input().split())
array = solution(x, d)
print(len(array))
print(' '.join(map(str, array)), end='')


(n, d) = [int(c) for c in input().split(' ')]
n_copy = n
groupsize_cases = [2 ** c - 1 for c in range(100)]
groups = []
i = 1
while n >= groupsize_cases[i]:
	i += 1
i -= 1
while n > 0:
	while n >= groupsize_cases[i]:
		groups.append(i)
		n -= groupsize_cases[i]
	i -= 1
ans = []
current_number = 1
for num in groups:
	ans.append(current_number)
	for i in range(num - 1):
		ans.append(current_number)
	current_number += d + 1
print(len(ans))
for num in ans:
	print(num, end=' ')


(x, d) = [int(i) for i in input().split()]
if x == 1:
	print(1)
	print(1)
	exit()
ind = 0
tmp = 1
counter = 0
ans = []
while x > 0:
	t = x & 1
	if t == 1:
		for j in range(ind):
			ans.append(tmp)
		counter += 1
		tmp += d + 1
	x >>= 1
	ind += 1
for i in range(counter):
	ans.append(ans[-1] + d + 1)
print(len(ans))
print(*ans)


(x, d) = map(int, input().split())
a = []
pot = 2 ** 30
e = 30
last = 0
while x > 0:
	while x < pot - 1:
		e -= 1
		pot //= 2
	x -= pot - 1
	a += [last + d for i in range(e)]
	last += d
print(len(a))
print(' '.join(map(str, a)))


import math
(x, d) = map(int, input().split())
n = math.floor(math.log(x + 1, 2))
if n - 1 >= d and x > 10000:
	diff = x
	g = 1
	arr = []
	c = n
	while diff > 0 and c <= 10000:
		for i in range(g, g + n):
			arr.append(g)
		g = g + d
		tb = 2 ** n - 1
		diff = diff - tb
		n = math.floor(math.log(diff + 1, 2))
		c += n
	if c > 10000:
		print(-1)
	else:
		print(c)
		print(*arr)
elif x <= 10000 or n - 1 >= d:
	print(x)
	for i in range(1, x * d + 2 - d, d):
		print(i, end=' ')
	print()
else:
	diff = x
	g = 1
	arr = []
	c = n
	while diff > 0:
		for i in range(g, g + n):
			arr.append(i)
		g = g + n + d - 1
		tb = 2 ** n - 1
		diff = diff - tb
		n = math.floor(math.log(diff + 1, 2))
		c += n
	if c > 10000:
		print(-1)
	else:
		print(c)
		print(*arr)


p = [1]

def powers_():
	global p
	for i in range(1, 31):
		p.append(p[i - 1] * 2)
	for i in range(1, 31):
		p[i] = p[i] - 1
powers_()
xn = input().split(' ')
x = int(xn[0])
vc = []
temp = 1
sum = 0
while x != 0:
	for i in range(30, 0, -1):
		if p[i] <= x:
			x = x - int(p[i])
			vc.append({'f': temp, 's': i})
			sum = sum + i
			break
	temp = temp + int(xn[1]) + 1
print(sum)
for i in range(0, len(vc)):
	for j in range(0, int(vc[i]['s'])):
		print(vc[i]['f'], end=' ')


(x, d) = map(int, input().split())
A = []
a = 1
k = 0
while a < x:
	a *= 2
a //= 2
x = bin(x)[2:][::-1]
y = 1
for i in range(len(x) - 1, -1, -1):
	if x[i] == '1':
		A += i * [y]
		k += 1
	y += d + 1
while k:
	A.append(y)
	y += d + 1
	k -= 1
print(len(A))
for i in A:
	print(i, end=' ')


s = [2 ** i - 1 for i in range(31, 0, -1)]
z = [i for i in range(31, 0, -1)]
(x, d) = map(int, input().split())
st = 1
p = 0
ret = []
while x > 0:
	while s[p] > x:
		p += 1
	for i in range(z[p]):
		ret.append(st)
	st += d
	x -= s[p]
if x != 0:
	print('-1')
else:
	print(len(ret))
	print(' '.join((str(i) for i in ret)))


(x, d) = list(map(int, input().split()))

def dmax(x):
	i = 0
	while 2 ** i - 1 <= x:
		i += 1
	return i - 1
tec = 1
n = 0
step = dmax(x)
stroka = ''
while step != 0:
	stroka += (str(tec) + ' ') * step
	tec += d
	x -= 2 ** step - 1
	n += step
	step = dmax(x)
print(n)
print(stroka)


import math
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class TreeNode:

	def __init__(self, k, v):
		self.key = k
		self.value = v
		self.left = None
		self.right = None
		self.parent = None
		self.height = 1
		self.num_left = 1
		self.num_total = 1

class AvlTree:

	def __init__(self):
		self._tree = None

	def add(self, k, v):
		if not self._tree:
			self._tree = TreeNode(k, v)
			return
		node = self._add(k, v)
		if node:
			self._rebalance(node)

	def _add(self, k, v):
		node = self._tree
		while node:
			if k < node.key:
				if node.left:
					node = node.left
				else:
					node.left = TreeNode(k, v)
					node.left.parent = node
					return node.left
			elif node.key < k:
				if node.right:
					node = node.right
				else:
					node.right = TreeNode(k, v)
					node.right.parent = node
					return node.right
			else:
				node.value = v
				return

	@staticmethod
	def get_height(x):
		return x.height if x else 0

	@staticmethod
	def get_num_total(x):
		return x.num_total if x else 0

	def _rebalance(self, node):
		n = node
		while n:
			lh = self.get_height(n.left)
			rh = self.get_height(n.right)
			n.height = max(lh, rh) + 1
			balance_factor = lh - rh
			n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
			n.num_left = 1 + self.get_num_total(n.left)
			if balance_factor > 1:
				if self.get_height(n.left.left) < self.get_height(n.left.right):
					self._rotate_left(n.left)
				self._rotate_right(n)
			elif balance_factor < -1:
				if self.get_height(n.right.right) < self.get_height(n.right.left):
					self._rotate_right(n.right)
				self._rotate_left(n)
			else:
				n = n.parent

	def _remove_one(self, node):
		replacement = node.left or node.right
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = replacement
			else:
				node.parent.right = replacement
			replacement.parent = node.parent
			node.parent = None
		else:
			self._tree = replacement
			replacement.parent = None
		node.left = None
		node.right = None
		node.parent = None
		self._rebalance(replacement)

	def _remove_leaf(self, node):
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = None
			else:
				node.parent.right = None
			self._rebalance(node.parent)
		else:
			self._tree = None
		node.parent = None
		node.left = None
		node.right = None

	def remove(self, k):
		node = self._get_node(k)
		if not node:
			return
		if AvlTree._is_leaf(node):
			self._remove_leaf(node)
			return
		if node.left and node.right:
			nxt = AvlTree._get_next(node)
			node.key = nxt.key
			node.value = nxt.value
			if self._is_leaf(nxt):
				self._remove_leaf(nxt)
			else:
				self._remove_one(nxt)
			self._rebalance(node)
		else:
			self._remove_one(node)

	def get(self, k):
		node = self._get_node(k)
		return node.value if node else -1

	def _get_node(self, k):
		if not self._tree:
			return None
		node = self._tree
		while node:
			if k < node.key:
				node = node.left
			elif node.key < k:
				node = node.right
			else:
				return node
		return None

	def get_at(self, pos):
		x = pos + 1
		node = self._tree
		while node:
			if x < node.num_left:
				node = node.left
			elif node.num_left < x:
				x -= node.num_left
				node = node.right
			else:
				return (node.key, node.value)
		raise IndexError('Out of ranges')

	@staticmethod
	def _is_left(node):
		return node.parent.left and node.parent.left == node

	@staticmethod
	def _is_leaf(node):
		return node.left is None and node.right is None

	def _rotate_right(self, node):
		if not node.parent:
			self._tree = node.left
			node.left.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.left
			node.left.parent = node.parent
		else:
			node.parent.right = node.left
			node.left.parent = node.parent
		bk = node.left.right
		node.left.right = node
		node.parent = node.left
		node.left = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	def _rotate_left(self, node):
		if not node.parent:
			self._tree = node.right
			node.right.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.right
			node.right.parent = node.parent
		else:
			node.parent.right = node.right
			node.right.parent = node.parent
		bk = node.right.left
		node.right.left = node
		node.parent = node.right
		node.right = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	@staticmethod
	def _get_next(node):
		if not node.right:
			return node.parent
		n = node.right
		while n.left:
			n = n.left
		return n
avl = AvlTree()

class SegmentTree1:

	def __init__(self, data, default='z', func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] <= key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater
(x, d) = map(int, input().split())
l = []
while x > 5:
	c = 1
	rt = 0
	while c - 1 <= x:
		c *= 2
		rt += 1
	c //= 2
	if l == []:
		rty = 1
	else:
		rty = l[-1] + d
	for i in range(rt - 1):
		l.append(rty)
	x -= c - 1
c = 1 - d
if len(l) != 0:
	c = l[-1]
for i in range(x):
	l.append(c + d)
	c += d
print(len(l))
print(*l, sep=' ')


from math import log2
(x, d) = map(int, input().split())
(n, v) = (0, 1)
arr = []
while x > 0:
	t = int(log2(x + 1))
	arr += [v] * t
	n += t
	v += d
	x -= (1 << t) - 1
print(n)
print(' '.join(map(str, arr)))


(X, d) = map(int, input().split())
x = X
ans = list()
ln = 0
last = 1
while x > 0:
	if last >= 10 ** 18:
		print(-1)
		exit()
	i = 0
	while 2 ** (i + 1) - 1 <= x:
		i += 1
	t = 2 ** i - 1
	x -= t
	if ln + i > 10000:
		print(-1)
		exit()
	for _ in range(i):
		ans.append(last)
		ln += 1
	last += d
print(len(ans))
print(' '.join(map(str, ans)))


import math
(x, d) = map(int, input().split())
a = []
while True:
	k = int(math.log2(x))
	a.append(k)
	x -= 2 ** k - 1
	if x == 1:
		a.append(1)
		break
k = 1
b = []
for x in a:
	for _ in range(x):
		b.append(str(k))
	k += d
print(len(b))
print(' '.join(b))


(x, d) = map(int, input().split())
result = []
added = 0
while x > 0:
	b = '{0:b}'.format(x + 1)
	result += [1 + added * d] * (len(b) - 1)
	x = x - (1 << len(b) - 1) + 1
	added += 1
if len(result) > 10000 or result[-1] >= 10 ** 18:
	print(-1)
else:
	print(len(result))
	print(' '.join(map(str, result)))


from math import log2, floor, ceil
(x, d) = input().split()
(x, d) = (int(x), int(d))
k = 1
arr = list()
c = 1
ans = True
while x > 0:
	p = floor(log2(x))
	if 2 ** (p + 1) - 1 <= x:
		p += 1
	if p == 0:
		ans = False
		break
	arr.extend([k] * p)
	k += d + 1
	x -= 2 ** p - 1
if ans and len(arr) <= 10000:
	print(len(arr))
	print(' '.join(list(map(str, arr))))
else:
	print(-1)


import sys
[X, d] = map(int, sys.stdin.readline().strip().split())
a = []
m = 1 - d
i = 31
while i > 0:
	if 2 ** i - 1 > X:
		i -= 1
		continue
	a.extend([m + d] * i)
	m += d
	X -= 2 ** i - 1
print(len(a))
print(' '.join(map(str, a)))


(x, k) = list(map(int, input().split()))
tot = 0
now = 1
d = 1
ans = []

def add(am, el):
	global ans
	ans += [el] * am

def rec(l):
	global d
	global tot
	now = 1
	while True:
		if tot + (2 ** now - 1) > x:
			if tot - len(ans) != 0:
				rec(x - len(ans))
			break
		tot += 2 ** now - 1
		add(now, d)
		now += 1
		d += k
		if tot == x:
			break
rec(x)
print(len(ans))
print(*ans)


ii = lambda : int(input())
kk = lambda : map(int, input().split())
ll = lambda : list(kk())
(x, d) = kk()
v = 1
s = []
for i in range(35):
	if 2 ** i > x:
		break
	if 2 ** i & x:
		s.extend([v] * i)
		s.append(v + 10 ** 9)
		v += 2 * 10 ** 9
print(len(s))
print(*s)


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
from collections import defaultdict
from itertools import permutations
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
(k, d) = map(int, input().split())
temp = list()
copy = k
while k > 0:
	m = k % 2
	temp.append(m)
	k //= 2
y = 1
r = list()
for i in range(len(temp)):
	if temp[i] == 1:
		for j in range(i):
			r.append(y)
		y += d
		r.append(y)
		y += d
print(len(r))
print(*r)


from collections import defaultdict as dd, deque
(x, d) = map(int, input().split())
Q = deque()
A = []
i = 1
while x > 0:
	n = 1
	while 2 ** (n + 1) - 1 <= x:
		n += 1
	x -= 2 ** n - 1
	A.extend([i] * n)
	i += 2 * d
print(len(A))
print(*A)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import factorial
from collections import Counter, defaultdict
from heapq import heapify, heappop, heappush

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)
mod = 998244353
INF = float('inf')
from math import log2, floor

def main():
	(x, d) = RL()
	res = []
	now = 1
	while x > 0:
		num = floor(log2(x + 1))
		res += [now] * num
		x -= 2 ** num - 1
		now += d + 1
	print(len(res))
	print(*res)
main()


(x, d) = map(int, input().split())
r = []
e = 29
b = 2 ** e - 1
while x:
	while x < b:
		e -= 1
		b >>= 1
	r += [str((len(r) + 1) * d)] * e
	x -= b
print(len(r))
print(' '.join(r))


import math
import sys
input = sys.stdin.readline

def main():
	(x, d) = [int(i) for i in input().split(' ')]
	ans = []
	cur_n = 1
	pos = 0
	while x > 0:
		if x % 2 == 1:
			for j in range(pos):
				ans.append(cur_n)
			cur_n += d + 5
			ans.append(cur_n)
			cur_n += d + 5
		x >>= 1
		pos += 1
	print(len(ans))
	for i in ans:
		print(i, end=' ')
main()


from sys import stdin, stdout
(X, d) = stdin.readline().strip().split(' ')
(X, d) = (int(X), int(d))
di = {}
ctr = 1
y = 0
ctr1 = 0
while 1 << y <= X:
	if 1 << y & X & (1 << y & X) - 1 == 0 and 1 << y & X != 0:
		ctr1 += y + 1
		di[ctr] = y
		ctr += d + 1
		di[ctr] = 1
		ctr += d + 1
	y += 1
stdout.write(str(ctr1) + '\n')
for i in di:
	stdout.write((str(i) + ' ') * di[i])


(x, d) = map(int, input().split())
c = 1
r = '\n'
e = 29
b = 2 ** e - 1
while x:
	while x < b:
		e -= 1
		b >>= 1
	r += f'{c * d} ' * e
	c += e
	x -= b
print(c - 1, r)


(x, d) = [int(i) for i in input().split()]
cr = 1
ans = []
while x:
	i = 0
	while (1 << i) - 1 <= x:
		i += 1
	for j in range(i - 1):
		ans.append(cr)
	x -= (1 << i - 1) - 1
	cr += d
print(len(ans))
print(*ans)


(x, d) = [int(s) for s in input().split()[:2]]

def getd(y):
	if y <= 1:
		return y
	da = 1
	num = 0
	while da * 2 <= y:
		da *= 2
		num += 1
	return num
f = 1
ans = []
while x > 0:
	num = getd(x)
	n = 2 ** num - 1
	for i in range(num):
		ans.append(f)
	f += d + 1
	x -= n
print(len(ans))
for i in ans[:-1]:
	print(i, end=' ')
print(ans[-1])


import itertools

def subsequence_counter(x, d):
	ans = []
	new = '{0:b}'.format(x)
	counter = 1
	for i in range(len(new)):
		if new[i] == '1':
			ans.extend(list(itertools.repeat(counter, len(new) - i - 1)))
			ans.append(d + counter + 1)
		if new[i] == '0':
			continue
		counter += 2 * d + counter + 1
	return (len(ans), ans)
(a, b) = input().split()
a = int(a)
b = int(b)
(c, d) = subsequence_counter(a, b)
print(c)
print(' '.join(map(str, d)))