Codeforces 960C - Subsequence Counting 【思维+构造】


C. Subsequence Counting

time limit per test  1second

memory limit per test     256megabytes

Pikachu had an array with him. He wrotedown all the non-empty subsequences of the array on paper. Note that an arrayof size n has 2n - 1 non-emptysubsequences in it.

Pikachu being mischievous as he alwaysis, removed all the subsequences in whichMaximum_element_of_the_subsequence - Minimum_element_of_subsequence ≥ d

Pikachu was finally left withX subsequences.

However, he lost the initial array hehad, and now is in serious trouble. He still remembers the numbersX and d. He now wantsyou to construct any such array which will satisfy the above conditions. Allthe numbers in the final array should be positive integers less than1018.

Note the number of elements in theoutput array should not be more than104. If no answer is possible, print  - 1.

Input

The only line of input consists of twospace separated integersX and d (1 ≤ X, d ≤ 109).

Output

Output should consist of two lines.

First line should contain a singleintegern (1 ≤ n ≤ 10 000)— the number ofintegers in the final array.

Second line should consist ofn space separatedintegers — a1, a2, ... , an (1 ≤ ai < 1018).

If there is no answer, print a single integer-1. If there are multiple answers, printany 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 withMaximum_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, theremaining 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.


【题目链接】 link


【题意】

我们只要含有n个元素的集合有2n-1个子集,现在限定子集内元素的最大值减去最小值小于等于d的才是合法的子集,现在给出子集个数和d,求原始集合


【思路】

由于d这个限制破坏了原来2n-1的规律,那么我们考虑解除d的影响。

把x类似于进制分解,把x分成若干个2i-1,即子集合,每个子集合内的数都相等(差值不超过d),不同子集合内的数大小超过d。这样就保证了每个子集合内产生的子集贡献即为2n-1

将x从高位到低位分解模拟即可。


#include <cstdio>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)

typedef long long ll;
const int maxn = 100005;
const ll mod = 1e9+7;
const ll INF = 1e18;
const double eps = 1e-6;

int x,d;

int main()
{
    scanf("%d%d",&x,&d);
    ll now=1;
    vector<ll>vec;
    for(int i=30;i>=0;i--)
    {
        while(x>=(1<<i)-1)
        {
            for(int j=0;j<i;j++) vec.push_back(now);
            x-=(1<<i)-1;
            now+=d+1;
            if(x==0) break;
        }
        if(x==0) break;
    }
    printf("%d\n",vec.size());
    for(auto it:vec) printf("%lld ",it);
    puts("");
}




 

发布了259 篇原创文章 · 获赞 100 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/my_sunshine26/article/details/79848324