Problem :
We are given a series of Binary Strings. The first term in this series is 0. Hence, T1 = '0'. To find the string at any given Ti, we look at Ti - 1 and replace all occurances of '0' with '01' and '1' with '10'. You are given two integers N and X, your job is to find TN and return its Xth index. Note: Ti are 1-indexed.
Input Format:
First line of input contains an integer T, representing the number of test cases. The next T lines contains two space separated integers, N and X.
Output Format:
Print the Xth index in Nth row
Sample Input:
1
4 5
Sample Output:
1
Explanation:
T1: '0'
T2: '01'
T3: '0110'
T4: '01101001'
The 5th element in T4 is '1'.
I tried below solution but getting time limit exceed for n value greater than 25.how to handle large test cases ?
from sys import stdin , setrecursionlimit
setrecursionlimit(10**8)
t = int(input())
def temp(s,n):
if n==0:
return s
ans = ''
d = {
'0':'01',
'1':'10'
}
for ele in s:
ans+=d[ele]
return temp(ans,n-1)
while t:
n,x = [int(x) for x in stdin.readline().strip().split()]
s = '0'
res = temp(s,n)
print(res[x-1])
t-=1