Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. 

Chef's team is going to participate at the legendary math battles. One of the main task in the competition is to calculate the number of ways to create a number by adding some Chefonacci numbers. A number is called a Chefonacci number if it is an element of Chefonacci sequence defined as follows.

f(0) = 1; 
f(1) = 2; 
For i > 1 : f(i) = f(i - 1) + f(i - 2)

Chef asked you to help him with this task. There will be Q question of form X, K : How many different ways are there to create X by adding K Chefonacci numbers. Note that the order of numbers in the addition does not matter, i.e. (f(i) + f(j) + f(k)) and (f(j) + f(i) + f(k)) will not be counted as distinct ways. Also note that you are allowed to use a Chefonacci number any number of times (zero or more).

As the answer could be large, print your answer modulo 10^{9} + 7 (1000000007).

------ Input ------ 

First line of the input contains an integer Q denoting number of questions Chef was asked.

In the next Q lines follow the questions, i-th of the line will denote the i-th question represented by two space separated integer X, K respectively.

------ Output ------ 

For each question, output a separate line containing the answer of the question.

------ 
------ Constraints -----
Subtask 1 : [10 points]  
1 ≤ Q ≤ 50 
1 ≤  X  ≤ 10^{9}  
1 ≤  K  ≤  4  

Subtask 2 : [20 points]  
1 ≤ Q ≤ 100 
1 ≤  X  ≤ 10^{9}  
1 ≤  K  ≤  5  

Subtask 3 : [20 points]
1 ≤ Q ≤ 100 
1 ≤  X  ≤ 10^{2}  
1 ≤  K  ≤  10  

Subtask 4 : [50 points]
1 ≤ Q ≤ 100 
1 ≤  X  ≤ 10^{9} 
1 ≤  K  ≤  10  

----- Sample Input 1 ------ 
5

12 1

13 1

13 2

13 3

13 4
----- Sample Output 1 ------ 
0

1

1

2

4
----- explanation 1 ------ 

Example case 1.
There is no way to create 12 by adding one Chefonacci number, as 12 is not a Chefonacci number.

Example case 2.
There is only one way to create 13 by adding one Chefonacci number, i.e. 13.

Example case 3.
There is one way to create 13 by adding two Chefonacci numbers, i.e. 5 + 8.

Example case 4.
There are two ways to create 13 by adding three Chefonacci numbers: 2 + 3 + 8, 3 + 5 + 5.

Example case 5.
There are four ways to create 13 by adding four Chefonacci numbers: 1 + 1 + 3 + 8, 1 + 2 + 2 + 8, 1 + 2 + 5 + 5, 2 + 3 + 3 + 5

Difficulty: HARD
Tags: Dynamic programming

Complete search
Skill types: Dynamic programming

Complete search

Starter code:

Solutions:
from collections import Counter
dp = [0] * 44
dp[0] = 1
dp[1] = 2
for i in range(2, 44):
	dp[i] = dp[i - 1] + dp[i - 2]

def solve(x, k, n):
	if k == 0:
		if x == 0:
			return 1
		else:
			return 0
	elif x == 0 or n == 0:
		return 0
	elif x >= 0 and x <= k * dp[n - 1]:
		return solve(x, k, n - 1) + solve(x - dp[n - 1], k - 1, n)
	return 0
T = int(input())
while T:
	T -= 1
	(x, k) = map(int, input().split())
	print(solve(x, k, 43))


chefonacci = [0] * 44
chefonacci[0] = 1
chefonacci[1] = 2
for i in range(2, 44):
	chefonacci[i] = chefonacci[i - 1] + chefonacci[i - 2]

def no_of_ways(x, k, index):
	if k == 0:
		if x == 0:
			return 1
		else:
			return 0
	elif x == 0 or index == 0:
		return 0
	elif x >= 0 and x <= k * chefonacci[index]:
		return no_of_ways(x, k, index - 1) + no_of_ways(x - chefonacci[index - 1], k - 1, index)
	return 0
for i in range(int(input())):
	(x, k) = map(int, input().split())
	global index_1
	for i in chefonacci:
		if i > x:
			index_1 = chefonacci.index(i)
	print(no_of_ways(x, k, index_1))


f = [1, 2]
f1 = 1
f2 = 2
while True:
	f3 = f2 + f1
	if f3 > 10 ** 9:
		break
	f.append(f3)
	f1 = f2
	f2 = f3

def F(x, k, n):
	if x < 0:
		return 0
	if k == 0:
		if x == 0:
			return 1
		else:
			return 0
	if x == 0 or n == 0:
		return 0
	total = F(x, k, n - 1)
	if x >= f[n - 1] and x <= k * f[n - 1]:
		total += F(x - f[n - 1], k - 1, n)
	return total
Q = int(input())
for q in range(Q):
	(X, K) = list(map(int, input().split()))
	print(F(X, K, len(f)))


def chefonacci(n):
	f = []
	for val in range(n):
		if val == 0:
			f.append(1)
		if val == 1:
			f.append(2)
		if val > 1:
			f.append(f[val - 1] + f[val - 2])
	return f

def F(x, k, n):
	if k > 0 and x < k * f[0] and (x >= 0):
		return 0
	if x < 0:
		return 0
	if K == 1:
		for val in f:
			if val == X:
				return 1
		return 0
	if k == 0:
		if x == 0:
			return 1
		else:
			return 0
	elif x == 0 or n == 0:
		return 0
	else:
		total = F(x, k, n - 1)
		if x >= f[n - 1] and x <= k * f[n - 1]:
			total += F(x - f[n - 1], k - 1, n)
		return total

def compute_answer(X, K):
	return F(X, K, 43)
f = chefonacci(44)
Q = int(input())
for case in range(Q):
	(X, K) = list(map(int, input().split()))
	number = compute_answer(X, K)
	print(number)


from functools import *

@lru_cache(maxsize=None)
def change(X, k, i):
	if chefo[i] * k < X:
		return 0
	if k == 0 and X == 0:
		return 1
	if k == 0 and X != 0:
		return 0
	if X < 0:
		return 0
	if X == 0 and k != 0:
		return 0
	if k == 1:
		if chefo[i] == X:
			return 1
	if i < 0:
		return 0
	return change(X - chefo[i], k - 1, i) + change(X, k, i - 1)
d = {1: 0, 2: 1}
chefo = [1, 2]
i = 2
while chefo[-1] < 10 ** 9:
	chefo.append(chefo[-1] + chefo[-2])
	d[chefo[-1]] = i
	i += 1
q = int(input())
for i in range(q):
	s = input().split()
	X = int(s[0])
	k = int(s[1])
	for j in range(len(chefo)):
		if chefo[j] > X:
			break
	print(change(int(s[0]), int(s[1]), j - 1) % 1000000007)


from functools import lru_cache
f = [1, 2]
mod = 10 ** 9 + 7
for i in range(42):
	f.append(f[-1] + f[-2])
q = int(input())

def dfs(x, k, pos):
	if k == 0:
		return x == 0
	ans = 0
	for i in range(pos, -1, -1):
		if f[i] * k < x:
			return ans
		if f[i] <= x:
			ans += dfs(x - f[i], k - 1, i)
	return ans
for _ in range(q):
	(x, k) = map(int, input().split())
	print(dfs(x, k, 42))


def solve(x, k, n):
	if k == 0:
		if x == 0:
			return 1
		else:
			return 0
	elif x == 0 or n == 0:
		return 0
	else:
		total = solve(x, k, n - 1)
		if x >= chef[n - 1] and x <= k * chef[n - 1]:
			total += solve(x - chef[n - 1], k - 1, n)
		return total
chef = [0] * 50
(chef[0], chef[1]) = (1, 2)
for i in range(2, 50):
	chef[i] = chef[i - 1] + chef[i - 2]
for _ in range(int(input())):
	(x, k) = map(int, input().split())
	print(solve(x, k, 43))


def solve(x, k, n):
	if k == 0:
		if x == 0:
			return 1
		else:
			return 0
	elif x == 0 or n == 0:
		return 0
	else:
		total = solve(x, k, n - 1)
		if x >= chef[n - 1] and x <= k * chef[n - 1]:
			total += solve(x - chef[n - 1], k - 1, n)
		return total
mod = int(1000000000.0 + 7)
chef = [0] * 45
(chef[0], chef[1]) = (1, 2)
for i in range(2, 45):
	chef[i] = chef[i - 1] + chef[i - 2]
for _ in range(int(input())):
	(x, k) = map(int, input().split())
	print(solve(x, k, 43) % mod)


import bisect
fib = [1, 2]
while fib[-1] <= 10 ** 9:
	fib.append(fib[-1] + fib[-2])

def decompose(n):
	ans = []
	while n != 0:
		k = bisect.bisect_right(fib, n)
		ans.append(k - 1)
		n -= fib[k - 1]
	return tuple(reversed(ans))

def alter(visited, unvisited, tup, remove, add):
	new_val = list(tup)
	for x in remove:
		new_val.remove(x)
	new_val = tuple(sorted(new_val + add))
	if new_val not in visited:
		unvisited.add(new_val)
t = int(input())
for _ in range(t):
	(n, k) = (int(i) for i in input().split())
	visited = set([])
	decomp = decompose(n)
	if len(decomp) > k:
		print(0)
		continue
	unvisited = {decomp}
	ct = 0
	while unvisited != set([]):
		x = unvisited.pop()
		visited.add(x)
		if len(x) < k:
			if 1 in x:
				alter(visited, unvisited, x, [1], [0, 0])
			for q in x:
				if q >= 2:
					alter(visited, unvisited, x, [q], [q - 1, q - 2])
		if 0 in x and 2 in x:
			alter(visited, unvisited, x, [0, 2], [1, 1])
		for q in x:
			if q - 3 in x:
				alter(visited, unvisited, x, [q - 3, q], [q - 1, q - 1])
		if len(x) == k:
			ct += 1
	print(ct)


import sys
sys.setrecursionlimit(10000000)
N = 1000000000
fib = [1, 2]
while True:
	val = fib[-1] + fib[-2]
	if val > N:
		break
	fib.append(val)

def getans(i, x, k):
	if x < 0 or (k == 0 and x > 0) or i < 0 or (k * fib[i] < x):
		return 0
	if x == 0 and k == 0:
		return 1
	ans = getans(i, x - fib[i], k - 1) + getans(i - 1, x, k)
	return ans
for _ in range(int(input())):
	(x, k) = list(map(int, input().split()))
	print(getans(len(fib) - 1, x, k))


chef = [1, 2]
i = 2
while chef[-1] < 10 ** 9:
	chef.append(chef[i - 1] + chef[i - 2])
	i += 1
cache = {}

def fetch(X, K, i):
	if X in cache:
		if K in cache[X]:
			if i in cache[X][K]:
				return cache[X][K][i]
	return -1

def store(X, K, i, val):
	if X not in cache:
		cache[X] = {}
	if K not in cache[X]:
		cache[X][K] = {i: val}

def compute(X, K, i):
	if X > chef[i] * K:
		return 0
	if X <= 0:
		return 1 if K == 0 else 0
	elif K == 0:
		return 0
	elif i < 0:
		return 0
	else:
		val = fetch(X, K, i)
		if val != -1:
			return val
		elif X - chef[i] < 0:
			val = compute(X, K, i - 1)
			store(X, K, i, val)
			return val
		else:
			val = compute(X - chef[i], K - 1, i) + compute(X, K, i - 1)
			store(X, K, i, val)
			return val
cl = len(chef) - 1
Q = int(input())
for _ in range(Q):
	(X, K) = input().split()
	(X, K) = (int(X), int(K))
	print(compute(X, K, cl))


mod = 10 ** 9 + 7
fib = [1, 2]
while fib[-1] < 10 ** 9:
	fib.append(fib[-1] + fib[-2])
fibs = set(fib)
cache = {}

def ways(num, parts, maxused):
	if num == 0 and parts == 0:
		return 1
	if parts == 0 or maxused == 1:
		return 0
	if parts * maxused < num:
		return 0
	if parts == 1:
		return 1 if num < maxused and num in fibs else 0
	if maxused > num and (num, parts) in cache:
		return cache[num, parts]
	res = 0
	i = 0
	while fib[i] <= num and fib[i] < maxused:
		k = 1
		while k * fib[i] <= num and k <= parts:
			res = res + ways(num - k * fib[i], parts - k, fib[i])
			k += 1
		i += 1
		res %= mod
	if maxused > num:
		cache[num, parts] = res
	return res
for _ in range(int(input())):
	(x, k) = map(int, input().split())
	print(ways(x, k, x + 1))


cn = [0] * 44
(cn[1], cn[0]) = (2, 1)
for i in range(2, 44):
	cn[i] = cn[i - 1] + cn[i - 2]

def findWays(x, k, ind):
	if k == 0:
		return 1 if x == 0 else 0
	elif x == 0 or ind == 0:
		return 0
	else:
		if x >= 0 and k * cn[ind] >= x:
			return findWays(x - cn[ind - 1], k - 1, ind) + findWays(x, k, ind - 1)
		return 0
q = int(input())
for _ in range(q):
	(x, k) = map(int, input().split())
	for i in cn:
		if i > x:
			ind = cn.index(i)
	print(findWays(x, k, ind))


import sys
import numpy as np
fib = np.zeros(43, dtype=np.int64)
fib[:2] = [1, 2]
for i in range(2, len(fib)):
	fib[i] = fib[i - 1] + fib[i - 2]
maxfib = max(fib)
lfib = len(fib)

def memo(f):
	D = dict()

	def g(*a):
		try:
			return D[a]
		except:
			x = f(*a)
			D[a] = x
			return x
	return g

@memo
def num_sums(x, k, mx=lfib):
	if k == 0:
		return 0
	if x < 0:
		return 0
	if x > fib[mx - 1] * k:
		return 0
	if k == 1:
		return 1 if x in fib[:mx] else 0
	return sum((num_sums(x - i, k - 1, j + 1) for (j, i) in enumerate(fib[:mx]) if x - i > 0))
f = sys.stdin
q = int(f.readline())
for i in range(q):
	print(num_sums(*list(map(int, f.readline().split()))))


MOD = 1000000007
fib = [1, 2]
for i in range(2, 50):
	fib.append(fib[i - 1] + fib[i - 2])

def solve(x, k, n):
	if k == 0:
		return int(x == 0)
	ans = 0
	for i in range(n, -1, -1):
		if fib[i] * k < x:
			return ans
		if fib[i] <= x:
			ans = (ans + solve(x - fib[i], k - 1, i)) % MOD
	return ans
for i in range(int(input().strip())):
	(x, k) = map(int, input().split())
	print(solve(x, k, 43))


for _ in range(int(input())):
	(x, k) = (int(x) for x in input().split())
	fib = [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733]

	def waifu(x, k, n):
		ans = 0
		if k == 0:
			return x == 0
		for i in range(n, -1, -1):
			if fib[i] * k < x:
				return ans
			if fib[i] <= x:
				ans += waifu(x - fib[i], k - 1, i)
		return ans
	print(waifu(x, k, 42))


fibo = [0] * 44
(fibo[0], fibo[1]) = (1, 2)
for i in range(2, 44):
	fibo[i] = fibo[i - 1] + fibo[i - 2]

def chefonnaci_number(x, k, idx):
	if k == 0:
		if x == 0:
			return 1
		else:
			return 0
	elif x == 0 or idx == 0:
		return 0
	elif x >= 0 and x <= k * fibo[idx - 1]:
		return chefonnaci_number(x, k, idx - 1) + chefonnaci_number(x - fibo[idx - 1], k - 1, idx)
	return 0
for _ in range(int(input())):
	(x, k) = map(int, input().split())
	print(chefonnaci_number(x, k, 43))