Graceful Prime Decomposition
InterviewTime limit1sMemory limit128 MB
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 as a sum of prime numbers. Let denote the number of ways to decompose using primes that are less than or equal to . That is, is written as a sum of primes:
Recall that the smallest prime is .
This decomposition carries one extra rule. The graceful rule requires that every pair of adjacent primes be different, that is for all . We call such a decomposition a Graceful Prime Decomposition (GPD), written compactly as .
Order matters: and are counted as two different decompositions.
For example, :
and :
Note that is not a valid GPD, because the adjacent pair is equal. Likewise is valid but is not. Compute for the given integers and .
Input
The first line contains the number of test cases . Each test case is a single line with two integers and , where .
Output
For each test case, print a single integer on its own line.