Fibonacci Strings 2
InterviewTime limit2sMemory limit1024 MB
For each test case, count the letter a among the first k characters of the Fibonacci string F_n, where F_0 is a, F_1 is b, and F_i is F_(i-2) followed by F_(i-1).
- Level
Medium5 of 10
- Topics
- Divide and conquer, Recursion, String
- Solved
- No attempts yet
Problem
Recurrence relations are often used in mathematics. They usually define numerical sequences, but they can also define sequences of strings.
One example of strings defined by a recurrence is the Fibonacci strings , , They are defined as follows: , , for . The first seven Fibonacci strings are a, b, ab, bab, abbab, bababbab, and abbabbababbab.
Dima attends an olympiad programming club and is interested in string algorithms. He recently learned about Fibonacci strings. He quickly realized that their length grows very fast as i increases, so finding all characters of requires too much memory. Last time he only looked for some characters of . This time he set himself a harder task: count how many times the letter a appears among the first characters of .
Write a program that solves this problem.
Input
The first line of the input contains the number of test cases (). Each of the next lines contains two integers and (, ), where is the length of the string . Character positions are numbered from 1.
Output
Print lines. Each line contains one number, the answer for the corresponding test case: the number of a characters among the first characters of .