Sum of Distinct Primes

Time limit1sMemory limit128 MB

Summary
Count subsets of k distinct primes summing to n, using dynamic programming over sieve-generated primes up to 1120.
Level

Medium5 of 10

Topics
Dynamic programming, Math, Number theory
Solved
No attempts yet

Problem

Every positive integer can be written as a sum of distinct primes. Given two integers nn and kk, write a program that counts the number of ways to write nn as a sum of kk distinct primes. Two representations that differ only in the order of the summands (for example 3+53+5 and 5+35+3) are considered the same and counted once.

For example, when n=24n=24 and k=3k=3 there are 2 ways: {2,3,19}\{2, 3, 19\} and {2,5,17}\{2, 5, 17\}. When n=24n=24 and k=2k=2 there are 3 ways: {5,19}\{5, 19\}, {7,17}\{7, 17\}, and {11,13}\{11, 13\}. When n=2n=2 and k=1k=1 there is 1 way: {2}\{2\}. When n=1n=1 and k=1k=1 the answer is 0 because 1 is not prime. Likewise, no two distinct primes sum to 4, so the answer for n=4n=4, k=2k=2 is also 0.

Input

The first line contains the number of test cases TT. Each test case is a single line containing two integers nn and kk separated by a space. (n≤1120n \le 1120, k≤14k \le 14)

Output

For each test case, print the number of ways on its own line. The answer is always less than 2312^{31}.

Examples1

  1. Example 1

    Input
    12
    24 3
    24 2
    2 1
    1 1
    4 2
    18 3
    17 1
    17 3
    17 4
    100 5
    1000 10
    1120 14
    
    Expected output
    2
    3
    1
    0
    0
    2
    1
    0
    1
    55
    200102899
    2079324314