Power Sum

0

0 votes
Dynamic Programming, Hard, Recruit, Two dimensional, 難しい, Algorithms, Approved
Problem

For an integer 'a' represented in decimal (an an-1 ... a2 a1), its powerSum( ) is defined as :

powerSum(a) = 2n-1 * an + 2n-2 * an-1 + .. + 2 * a2 + 1 * a1

You are required to find how many values of 'a' satisfy the following condition:
powerSum(a) <= powerSum(K) for all 0 <= 'a' <= N

Input:

The first line of input consists of an integer 'T' , indicating the number of test cases.
Each of the following 'T' lines contains two integers separated by a space - 'K' and 'N'.

Output:

For each test case, output an integer indicating number of Integers satisfying above condition up to given N (including N).
Answer for each test case should come in a new line.

Constraints:

39711 ≤ T ≤ 105
1 ≤ K,N ≤ 1015

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For Case 1 : K = 1, N = 10 , there are two such numbers 0,1

For Case 2 : K = 5, N = 10, since powerSum(K) = 5*1 = 5, there are 7 such numbers : 0,1,2,3,4,5,10

Editor Image

?