Stairs (Codeforces 1419B)
Tags: Greedy, brute force

Jett is tired after destroying the town and she wants to have a rest. She likes high places, that's why for having a rest she wants to get high and she decided to craft staircases.A staircase is a squared figure that consists of square cells. Each staircase consists of an arbitrary number of stairs. If a staircase has $$$n$$$ stairs, then it is made of $$$n$$$ columns, the first column is $$$1$$$ cell high, the second column is $$$2$$$ cells high, $$$\ldots$$$, the $$$n$$$-th column if $$$n$$$ cells high. The lowest cells of all stairs must be in the same row.A staircase with $$$n$$$ stairs is called nice, if it may be covered by $$$n$$$ disjoint squares made of cells. All squares should fully consist of cells of a staircase. This is how a nice covered staircase with $$$7$$$ stairs looks like:  Find out the maximal number of different nice staircases, that can be built, using no more than $$$x$$$ cells, in total. No cell can be used more than once.

Input: The first line contains a single integer $$$t$$$ $$$(1 \le t \le 1000)$$$  — the number of test cases.The description of each test case contains a single integer $$$x$$$ $$$(1 \le x \le 10^{18})$$$  — the number of cells for building staircases.

Output: For each test case output a single integer  — the number of different nice staircases, that can be built, using not more than $$$x$$$ cells, in total.

Note: In the first test case, it is possible to build only one staircase, that consists of $$$1$$$ stair. It's nice. That's why the answer is $$$1$$$.In the second test case, it is possible to build two different nice staircases: one consists of $$$1$$$ stair, and another consists of $$$3$$$ stairs. This will cost $$$7$$$ cells. In this case, there is one cell left, but it is not possible to use it for building any nice staircases, that have not been built yet. That's why the answer is $$$2$$$.In the third test case, it is possible to build only one of two nice staircases: with $$$1$$$ stair or with $$$3$$$ stairs. In the first case, there will be $$$5$$$ cells left, that may be used only to build a staircase with $$$2$$$ stairs. This staircase is not nice, and Jett only builds nice staircases. That's why in this case the answer is $$$1$$$. If Jett builds a staircase with $$$3$$$ stairs, then there are no more cells left, so the answer is $$$1$$$ again.

Sample Inputs:
4
1
8
6
1000000000000000000

Sample Outputs:
1
2
1
30

Solution (Python):
d = {1: 1, 3: 6, 7: 28, 15: 120, 31: 496, 63: 2016, 127: 8128, 255: 32640, 511: 130816, 1023: 523776, 2047: 2096128, 4095: 8386560, 8191: 33550336, 16383: 134209536, 32767: 536854528, 65535: 2147450880, 131071: 8589869056, 262143: 34359607296, 524287: 137438691328, 1048575: 549755289600, 2097151: 2199022206976, 4194303: 8796090925056, 8388607: 35184367894528, 16777215: 140737479966720, 33554431: 562949936644096, 67108863: 2251799780130816, 134217727: 9007199187632128, 268435455: 36028796884746240, 536870911: 144115187807420416, 1073741823: 576460751766552576}
l = list(d.values())
t = int(input())
for _ in range(t):
    x = int(input())
    c = 0
    for i in l:
        if x - i >= 0:
            x -= i
            c += 1
        else:
            break
    print(c)

Solution (Python):
import sys
input = sys.stdin.readline

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

def readInt():
    return int(input())

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

def readStr():
    return input().strip()

def solve():
    x = readInt()
    cnt = ans = 0
    for i in range(1, 32):
        cnt += (pow(2, i) - 1) * pow(2, i) // 2
        if cnt <= x:
            ans = i
        else:
            break
    return ans
for _ in range(int(input())):
    print(solve())

Solution (Python):
t = int(input())
for i in range(t):
    x = int(input())
    j = 0
    q = 0
    a = 2 ** j * 2 - 1
    u = a * (1 + a) // 2
    s = u
    while s <= x:
        j += 1
        a = 2 ** j * 2 - 1
        u = a * (1 + a) // 2
        q += 1
        s += u
    print(q)