Codeforces 960C 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 2n - 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 1018.

Note the number of elements in the output array should not be more than 104. 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 ≤ 109).

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 — a1, a2, ... , an (1 ≤ ai < 1018).

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.


题意:给你一个数x和一个d表示的是原来的序列的子序列的个数和一个子序列的元素中最大的元素-最小的元素的差值小于d.

然后让你构造原序列,如果有就先输出有几个然后输出分别是那几个。如果没有就输出-1.

解题思路:这道题一开始没补上,然后在课堂推到了一下公式之后就知道这道题目应该怎么样去写。题目上第一锻有描述。

我们每一次可以找到比当前x要小的最大连续序列的个数是多少。然后我们用相同的数b-w 重复进行求解。如果个数大于1e4的话就说明不行。我们从1开始,结束一次后加d+1.


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=1e5+10;
long long n,x,d;
struct Node{
	long long k,sum;
}node[maxn];
int main(){
	int i,j;
	scanf("%I64d%I64d",&x,&d);
	n=0;
	long long ans=0;
	long long cnt=0,flag=1;
	while(x){
		cnt++;
		for(i=0;((1<<(i+1))-1)<=x&&i<31;i++){
		}
		x-=((1<<i)-1);
		node[n].k=flag;node[n++].sum=i;
		ans+=i;
		flag+=(d+1);
	}
	if(ans>10000)
		printf("-1\n");
	else{
		printf("%I64d\n",ans);
		int flag=0;
		for(i=0;i<n;i++){
			for(j=0;j<node[i].sum;j++){
				if(flag) printf(" ");
				printf("%I64d",node[i].k);
				flag=1;
			}
		}
		printf("\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/bao___zi/article/details/79897571