Olya and magical square (Codeforces 1080D)
Tags: Implement, constructive algorithms

Recently, Olya received a magical square with the size of $$$2^n\times 2^n$$$.It seems to her sister that one square is boring. Therefore, she asked Olya to perform exactly $$$k$$$ splitting operations.A Splitting operation is an operation during which Olya takes a square with side $$$a$$$ and cuts it into 4 equal squares with side $$$\dfrac{a}{2}$$$. If the side of the square is equal to $$$1$$$, then it is impossible to apply a splitting operation to it (see examples for better understanding).Olya is happy to fulfill her sister's request, but she also wants the condition of Olya's happiness to be satisfied after all operations.The condition of Olya's happiness will be satisfied if the following statement is fulfilled:Let the length of the side of the lower left square be equal to $$$a$$$, then the length of the side of the right upper square should also be equal to $$$a$$$. There should also be a path between them that consists only of squares with the side of length $$$a$$$. All consecutive squares on a path should have a common side.Obviously, as long as we have one square, these conditions are met. So Olya is ready to fulfill her sister's request only under the condition that she is satisfied too. Tell her: is it possible to perform exactly $$$k$$$ splitting operations in a certain order so that the condition of Olya's happiness is satisfied? If it is possible, tell also the size of the side of squares of which the path from the lower left square to the upper right one will consist.

Input: The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^3$$$) — the number of tests.Each of the following $$$t$$$ lines contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 10^9, 1 \le k_i \le 10^{18}$$$) — the description of the $$$i$$$-th test, which means that initially Olya's square has size of $$$2^{n_i}\times 2^{n_i}$$$ and Olya's sister asks her to do exactly $$$k_i$$$ splitting operations.

Output: Print $$$t$$$ lines, where in the $$$i$$$-th line you should output "YES" if it is possible to perform $$$k_i$$$ splitting operations in the $$$i$$$-th test in such a way that the condition of Olya's happiness is satisfied or print "NO" otherwise. If you printed "YES", then also print the $$$log_2$$$ of the length of the side of the squares through space, along which you can build a path from the lower left square to the upper right one.You can output each letter in any case (lower or upper).If there are multiple answers, print any.

Note: In each of the illustrations, the pictures are shown in order in which Olya applied the operations. The recently-created squares are highlighted with red.In the first test, Olya can apply splitting operations in the following order:   Olya applies one operation on the only existing square. The condition of Olya's happiness will be met, since there is a path of squares of the same size from the lower left square to the upper right one:  The length of the sides of the squares on the path is $$$1$$$. $$$log_2(1) = 0$$$.In the second test, Olya can apply splitting operations in the following order:   Olya applies the first operation on the only existing square. She applies the second one on the right bottom square. The condition of Olya's happiness will be met, since there is a path of squares of the same size from the lower left square to the upper right one:  The length of the sides of the squares on the path is $$$2$$$. $$$log_2(2) = 1$$$.In the third test, it takes $$$5$$$ operations for Olya to make the square look like this:  Since it requires her to perform $$$7$$$ splitting operations, and it is impossible to perform them on squares with side equal to $$$1$$$, then Olya cannot do anything more and the answer is "NO".

Sample Inputs:
3
1 1
2 2
2 12

Sample Outputs:
YES 0
YES 1
NO

Solution (Python):
from sys import stdin, stdout
readline = stdin.readline
write = stdout.write
squareMoves = []

def precompute():
    top = 10 ** 18
    prev = 0
    while prev <= 30 * top:
        squareMoves.append(prev)
        prev = 1 + 4 * prev

def getAns(k, n):
    low = 0
    high = 0
    a = 1
    b = 1
    i = 1
    while i <= n:
        low += a
        high += a + b * squareMoves[n - i]
        if high >= k:
            if low > k:
                return -1
            return i
        a = 2 * a + 1
        b = 2 * a - 1
        i += 1
    return -1
if __name__ == '__main__':
    precompute()
    t = int(readline().strip())
    for i in range(t):
        [n, k] = list(map(int, readline().strip().split(' ')))
        tmpN = min(n, len(squareMoves))
        ans = getAns(k, tmpN)
        if ans == -1:
            write('NO\n')
        else:
            write('YES ' + str(n - ans) + '\n')

Solution (Python):
t = int(input())
for i in range(0, t):
    (n, k) = [int(x) for x in input().split()]
    if n >= 32:
        print('YES %d' % (n - 1))
    else:
        low = 0
        co = -1
        md = [0]
        for j in range(1, n):
            md.append(md[-1] * 4 + 1)
        kk = 0
        found = 0
        for cut in range(1, n + 1):
            low += (1 << cut) - 1
            co = 2 * co + 3
            kk += co * md[n - cut]
            if k >= low and k <= low + kk:
                print('YES %d' % (n - cut))
                found = 1
                break
        if found == 0:
            print('NO')

Solution (Python):
q = int(input())
for i in range(q):
    (n, k) = map(int, input().split())
    if n > 31:
        print('YES', n - 1)
        continue
    a = [0]
    for i in range(1, n + 1):
        a.append(a[i - 1] * 4 + 1)
    if a[n] < k:
        print('NO')
        continue
    if n == 2 and k == 3:
        print('NO')
        continue
    p = 0
    q = 2
    while p + q - 1 <= k and n > 0:
        p += q - 1
        q *= 2
        n -= 1
    print('YES', n)