zoukankan      html  css  js  c++  java
  • 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
    Copy
    10 5
    Output
    Copy
    6
    5 50 7 15 6 100
    Input
    Copy
    4 2
    Output
    Copy
    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,然后找共需要多少个数。

     1 #pragma warning(disable:4996)
     2 #include<bitset>
     3 #include<stack>
     4 #include<queue>
     5 #include<vector>
     6 #include<string>
     7 #include<cstdio>
     8 #include<cstring>
     9 #include<iostream>
    10 #include<algorithm>
    11 using namespace std;
    12 typedef long long ll;
    13 
    14 ll x, d;
    15 
    16 int main()
    17 {
    18     while (cin >> x >> d) {
    19         stack<int> s;
    20         ll ans = 0;
    21         ll sum = 0;
    22         for (int i = 0; (1 << i) <= x; i++) if ((x >> i) & 1) {
    23             ans += (1 << i) - 1;
    24             sum += i;
    25             s.push(i);
    26         }
    27         cout << sum + x - ans << endl;
    28     
    29         int k = 0;
    30         while (!s.empty()) {
    31             int t = s.top();
    32             s.pop();
    33             for (int i = 0; i < t; i++) cout << 1 + k * d << " ";
    34             k++;
    35         }
    36         
    37         for (int i = 0; i < x - ans; i++) {
    38             k++;
    39             cout << 1 + k * d << " ";
    40         }
    41         cout << endl;
    42     }
    43     return 0;
    44 }
  • 相关阅读:
    线程实现的两种方式
    webhook功能概述
    Linux 常用高频命令
    Mac安装Homebrew的正确姿势
    Mac 环境变量配置
    详解k8s零停机滚动发布微服务
    详解k8s原生的集群监控方案(Heapster+InfluxDB+Grafana)
    白话kubernetes的十万个为什么(持续更新中...)
    一个典型的kubernetes工作流程
    k8s实战之从私有仓库拉取镜像
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/8798892.html
Copyright © 2011-2022 走看看