Under construction

Still being built. Things may change

Graceful Prime Decomposition

Interview

Time limit1sMemory limit128 MB

Summary
Count ordered sums of N from primes up to K where no two neighboring primes are equal.
Level

Medium5 of 10

Topics
Dynamic programming, Number theory
Solved
No attempts yet

Problem

We want to express a positive integer NN as a sum of prime numbers. Let G(N,K)G(N, K) denote the number of ways to decompose NN using primes pip_i that are less than or equal to KK. That is, NN is written as a sum of primes:

N=p1+p2+p3+⋯+pr,(pi≤K)N = p_1 + p_2 + p_3 + \cdots + p_r, \quad (p_i \le K)

Recall that the smallest prime is 22.

This decomposition carries one extra rule. The graceful rule requires that every pair of adjacent primes be different, that is pi≠pi+1p_i \ne p_{i+1} for all ii. We call such a decomposition a Graceful Prime Decomposition (GPD), written compactly as N=(p1,p2,p3,…,pr)N = (p_1, p_2, p_3, \ldots, p_r).

Order matters: 2+52 + 5 and 5+25 + 2 are counted as two different decompositions.

For example, G(7,5)=3G(7, 5) = 3:

  • 7=2+3+2→(2,3,2)7 = 2 + 3 + 2 \rightarrow (2, 3, 2)
  • 7=2+5→(2,5)7 = 2 + 5 \rightarrow (2, 5)
  • 7=5+2→(5,2)7 = 5 + 2 \rightarrow (5, 2)

and G(5,5)=3G(5, 5) = 3:

  • 5=2+3→(2,3)5 = 2 + 3 \rightarrow (2, 3)
  • 5=3+2→(3,2)5 = 3 + 2 \rightarrow (3, 2)
  • 5=5→(5)5 = 5 \rightarrow (5)

Note that 7=2+2+37 = 2 + 2 + 3 is not a valid GPD, because the adjacent pair 2+22 + 2 is equal. Likewise (2,3,2)(2, 3, 2) is valid but (3,2,2)(3, 2, 2) is not. Compute G(N,K)G(N, K) for the given integers NN and KK.

Input

The first line contains the number of test cases TT. Each test case is a single line with two integers NN and KK, where 2≤N,K≤502 \le N, K \le 50.

Output

For each test case, print a single integer G(N,K)G(N, K) on its own line.

Examples1

  1. Example 1

    Input
    3
    7 5
    5 5
    8 2
    
    Expected output
    3
    3
    0