Decibinary Numbers


Problem Statement :


Let's talk about binary numbers. We have an n-digit binary number, b, and we denote the digit at index  i(zero-indexed from right to left) to be bi. We can find the decimal value of b using the following formula:
   (b)2 => bn-1.2^(n-1) + ... + b2 . 2^2+ b1.2^1 + b0.2^0 = (?)10
For example, if binary number b = 10010, we compute its decimal value like so:
   (10010)2 => 1.2^4 + 0.2^3 + 0.2^2 + 1.2^1 + 0.2^0 = (18)10
Meanwhile, in our well-known decimal number system where each digit ranges from 0 to 9, the value of some decimal number, d, can be expanded in the same way:
   d = d(n-1).10^n-1 + ... + d1.10^2 + d1.1^1 + d0.10^0
Now that we've discussed both systems, let's combine decimal and binary numbers in a new system we call decibinary! In this number system, each digit ranges from 0 to 9 (like the decimal number system), but the place value of each digit corresponds to the one in the binary number system. For example, the decibinary number 2016 represents the decimal number 24 because:
   (2016)decibinary => 2.2^3 + 0.2^2 + 1.2^1 + 6.2^0 = (24)10
Pretty cool system, right? Unfortunately, there's a problem: two different decibinary numbers can evaluate to the same decimal value! For example, the decibinary number 2008 also evaluates to the decimal value 24:
   (2008)decibinary => 2.2^3 + 0.2^2 + 02^1 + 8.2^0 = (24)10
This is a major problem because our new number system has no real applications beyond this challenge!

Consider an infinite list of non-negative decibinary numbers that is sorted according to the following rules:

1.The decibinary numbers are sorted in increasing order of the decimal value that they evaluate to.
2.Any two decibinary numbers that evaluate to the same decimal value are ordered by increasing decimal value, meaning the equivalent decibinary values are strictly interpreted and compared as decimal values and the smaller decimal value is ordered first. For example, (2)decibinary and (10)decibinary both evaluate to (2)10. We would order (2)decibinary before (10)decibinary because (2)10 < (10)10.
Here is a list of first few decibinary numbers properly ordered:

image

You will be given q queries in the form of an integer, x. For each x, find and print the the xth decibinary number in the list on a new line.

Function Description

Complete the decibinaryNumbers function in the editor below. For each query, it should return the decibinary number at that one-based index.

decibinaryNumbers has the following parameter(s):

x: the index of the decibinary number to return
Input Format

The first line contains an integer, q, the number of queries.
Each of the next q lines contains an integer, x, describing a query.

Constraints

1 <= q <= 10^5
1 <= x <= 10^16



Solution :



title-img


                            Solution in C :

In C++ :






#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

#define next next___

int s2[21];
unsigned long long Q[21][2600000];
unsigned long long W[2600000];
const int D = 20;

int main() {
    s2[0] = 1;
    for (int i = 1; i <= D; i++) {
        s2[i] = s2[i - 1] * 2;
    }
    Q[0][0] = 1;
    for (int i = 1; i <= D; i++) {
        int summ = min(9 * s2[i], 2600000);
        for (int sum = 0; sum < summ; sum++) {
            if (Q[i - 1][sum]) {
                for (int d = 0; d < 10; d++) {      
                    if (sum + d * s2[i - 1] < 2600000) {                              
                        Q[i][sum + d * s2[i - 1]] += Q[i - 1][sum];                
                    }
                }
            }
        }
    }

    W[0] = 1;
    for (int i = 1; i < 2600000; i++) {
        W[i] = W[i - 1] + Q[D][i];
    }        
    int q;
    cin >> q;
    while (q) {
        q--;
        unsigned long long x;
        cin >> x;
        if (x == 1) {
            cout << 0 << endl;
            continue;
        }
        int le = 0;
        int ri = 2600000;
        while (le + 1 < ri) {
            int mi = (le + ri) / 2;
            if (W[mi] >= x) {
                ri = mi;
            } else {
                le = mi;
            }
        }        
        x -= W[le];        
        long long ans = 0;
        for (int i = D; i >= 1; i--) {
            for (int d = 0; d < 10; d++) {
                if (Q[i - 1][ri - d * s2[i - 1]] >= x) {
                    ri -= d * s2[i - 1];
                    ans = ans * 10 + d;
                    break;
                }
                x -= Q[i - 1][ri - d * s2[i - 1]];
            }
        }
        cout << ans << endl;
    }
    
	return 0;
}








In Java :






import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        int BITS = 19;
        int MAX = 1<<BITS;
        long[][] dp = new long[BITS][MAX];
        for (int i = 0; i < 10; i++)
            dp[0][i] = 1;
        for (int i = 1; i < BITS; i++) {
            int sub = 1<<i;
            for (int j = 0; j < MAX; j++) {
                for (int k = 0; k <= 9; k++) {
                    int rem = j-k*sub;
                    if (rem < 0)
                        break;
                    dp[i][j] += dp[i-1][rem];
                }
            }
        }
        long sum = 0;
        TreeMap<Long, Integer> sums = new TreeMap<Long, Integer>();
        sums.put(0l, -1);
        for (int i = 0; i < MAX; i++) {
            sum += dp[BITS-1][i];
            sums.put(sum, i);
        }
        Scanner sc = new Scanner(System.in);
        int q = sc.nextInt();
        for (int z = 0; z < q; z++) {
            long n = sc.nextLong();
            int val = sums.floorEntry(n-1).getValue()+1;
            n -= sums.floorEntry(n-1).getKey();
            long ans = 0;
            for (int i = BITS-1; i > 0; i--) {
                int digit = 0;
                while (dp[i-1][val] < n) {
                    n -= dp[i-1][val];
                    digit++;
                    val -= (1<<i);
                }
                ans *= 10;
                ans += digit;
            }
            ans *= 10;
            ans += val;
            System.out.println(ans);
        }
    }
}








In C :






#include <stdio.h>
#include <stdlib.h>
#define MAX 500000
int get_i(long long*a,long long num,int size);
long long med(long long*a,int size);
long long dp[30][MAX],sum[MAX],two[30];
unsigned long long ten[30];

int main(){
  int T,i,j,k;
  long long x,y,z;
  unsigned long long ans;
  for(i=two[0]=ten[0]=1;i<30;i++){
    two[i]=two[i-1]*2;
    ten[i]=ten[i-1]*10;
  }
  for(i=0,dp[0][0]=1;i<MAX;i++)
    for(j=1;j<30;j++)
      for(k=0;k<10;k++)
        if(k*two[j-1]<=i)
          dp[j][i]+=dp[j-1][i-k*two[j-1]];
  for(i=0;i<MAX;i++)
    if(i)
      sum[i]=sum[i-1]+dp[29][i];
    else
      sum[i]=dp[29][i];
  scanf("%d",&T);
  while(T--){
    scanf("%lld",&x);
    i=get_i(sum,x,MAX);
    if(i)
      y=x-sum[i-1];
    else
      y=x;
      //printf("i:%d y:%lld\n",i,y);
    for(j=29,ans=0;j>=0;j--)
      if(j)
        for(k=z=0;k<10;k++){
          z+=dp[j][i-k*two[j]];
          if(z>=y){
            y-=z-dp[j][i-k*two[j]];
            i-=k*two[j];
            ans+=k*ten[j];
      //printf("i:%d y:%lld\n",i,y);
            break;
          }
        }
      else
        ans+=i;
    printf("%llu\n",ans);
  }
  return 0;
}
int get_i(long long*a,long long num,int size){
  if(size==0)
    return 0;
  if(num>med(a,size))
    return get_i(&a[(size+1)>>1],num,size>>1)+((size+1)>>1);
  else
    return get_i(a,num,(size-1)>>1);
}
long long med(long long*a,int size){
  return a[(size-1)>>1];
}








In Python3 :





#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the decibinaryNumbers function below.
power2=[1,2,4]

def init():
    
    #nb[decimal][m bits]

    d=[]
    nb=[]
    nb.append([])
    nb[0].append(1)
    d.append(1)
    nb.append([])
    nb[1].append(1)
    d.append(2)
    ind=2
    val=2
    decmax=9
    mmin=0
    mmax=1

    profondeur=16
    while ind<(10**profondeur):
        #nb[val]
        nb.append([])

        if val>decmax:
            mmin+=1
            decmax+=9*(power2[mmin])
        if val>=power2[mmax+1]:
            mmax+=1
            power2.append(power2[mmax]*2)
        if mmin>0:nb[val]=[0]*mmin
        else:nb[val].append(1)
        for m in range(max(mmin,1),mmax+1):
            temp,j,resul=val,0,0
            while temp>=0 and j<10:
                if (m-1)<len(nb[temp]):
                    resul+=nb[temp][m-1]
                else: resul+=nb[temp][-1]
                temp-=power2[m]
                j+=1
            nb[val].append(resul)
        ind+=nb[val][-1]
        d.append(ind)
        nb.append([])
        nb[val+1]=nb[val][:]
        ind+=nb[val+1][-1]
        d.append(ind)
        val+=2

    return d,nb

def decibinaryNumbers(x,d,nb):
    
    lowBound,highBound=0,len(d)
    while lowBound<highBound:
        mil=(lowBound+highBound)//2
        if d[mil]<x:
            lowBound=mil+1
        else:
            highBound=mil
    
    val=lowBound
    ind=d[val]
    if val==0:deb=0
    else:deb=d[val-1]

    return calcul(extraire(x-deb,val,nb))
    
def extraire(num,val,nb):
    if val==0: return [0]

    ind,nbBits=0,0
    while(num>nb[val][nbBits]): 
        nbBits+=1

    if nbBits==0:return [val]
    
    resul=[0]*(nbBits+1)    
    msb=0

    somme=nb[val][nbBits-1]
    prevVal,prevSomme,temp=0,0,val

    while(num>somme):
        msb+=1
        prevVal=temp
        temp-=power2[nbBits]
        prevSomme=somme
        if (nbBits-1)<len(nb[temp]):
            somme+=nb[temp][nbBits-1]
        else: somme+=nb[temp][-1]
    
    resul[-1]=msb
    aajouter=extraire(num-prevSomme,temp,nb)
    
    for i in range(len(aajouter)): resul[i]=aajouter[i]

    return resul

def calcul(liste):
    
    resul=0
    power10=1
    for l in liste:
        resul+=l*power10
        power10*=10
    return resul


if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    q = int(input())
    d,nb=init()

    for q_itr in range(q):
        x = int(input())

        result = decibinaryNumbers(x,d,nb)

        fptr.write(str(result) + '\n')

    fptr.close()
                        








View More Similar Problems

Equal Stacks

ou have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height. You can change the height of a stack by removing and discarding its topmost cylinder any number of times. Find the maximum possible height of the stacks such that all of the stacks are exactly the same height. This means you must remove zero or more cylinders from the top of zero or more of

View Solution →

Game of Two Stacks

Alexa has two stacks of non-negative integers, stack A = [a0, a1, . . . , an-1 ] and stack B = [b0, b1, . . . , b m-1] where index 0 denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack A or stack B. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified f

View Solution →

Largest Rectangle

Skyline Real Estate Developers is planning to demolish a number of old, unoccupied buildings and construct a shopping mall in their place. Your task is to find the largest solid area in which the mall can be constructed. There are a number of buildings in a certain two-dimensional landscape. Each building has a height, given by . If you join adjacent buildings, they will form a solid rectangle

View Solution →

Simple Text Editor

In this challenge, you must implement a simple text editor. Initially, your editor contains an empty string, S. You must perform Q operations of the following 4 types: 1. append(W) - Append W string to the end of S. 2 . delete( k ) - Delete the last k characters of S. 3 .print( k ) - Print the kth character of S. 4 . undo( ) - Undo the last (not previously undone) operation of type 1 or 2,

View Solution →

Poisonous Plants

There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plan

View Solution →

AND xor OR

Given an array of distinct elements. Let and be the smallest and the next smallest element in the interval where . . where , are the bitwise operators , and respectively. Your task is to find the maximum possible value of . Input Format First line contains integer N. Second line contains N integers, representing elements of the array A[] . Output Format Print the value

View Solution →