CZ_PROB1 - Summing to a Square Prime

$S_{P2} = \{p \mid p: \mathrm{prime} \wedge (\exists x_1, x_2 \in \mathbb{Z}, p = x_1^2 + x_2^2) \}$ is the set of all primes that can be represented as the sum of two squares. The function $S_{P2}(n)$ gives the $n$th prime number from the set $S_{P2}$. Now, given two integers $n$ ($0 < n < 501$) and $k$ ($0 < k < 4$), find $p(S_{P2}(n), k)$ where $p(a, b)$ gives the number of unordered ways to sum to the given total ‘$a$’ with ‘$b$’ as its largest possible part. For example: $p(5, 2) = 3$ (i.e. $2+2+1$, $2+1+1+1$, and $1+1+1+1+1$). Here $5$ is the total with $2$ as its largest possible part.

Input

The first line gives the number of test cases $T$ followed by $T$ lines of integer pairs, $n$ and $k$.

Constraints

  • $0 < T < 501$
  • $0 < n < 501$
  • $1 < S_{P2}(n) < 7994$
  • $0 < k < 4$

Output

The $p(S_{P2}(n), k)$ for each $n$ and $k$. Append a newline character to every test cases’ answer.

Example

Input:
3
2 2
3 2
5 3

Output:
3
7
85

Added by:Rahul
Date:2007-03-10
Time limit:1s
Source limit:3000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:Sam Collins

hide comments
2020-04-11 17:37:45
If there is a rendering issue, Read the problem here https://vjudge.net/problem/SPOJ-CZ_PROB1
2019-01-16 23:58:12
There seems to be a problem with the rendering of the mathematical notation. The first line reads:
$S_{P2} = \{p \mid p: \mathrm{prime} \wedge (\exists x_1, x_2 \in \mathbb{Z}, p = x_1^2 + x_2^2) \}$ is the set of all
Could an admin please take a look?
2018-05-31 18:37:24
Good question..
2017-06-19 18:39:42
do not forget to consider 2 in your set although its not congruent to 1 modulo 4
2017-06-15 14:05:19
due to small constraints can be solved without dp also ;) #PNC

Last edit: 2017-06-15 14:12:20
2017-04-27 14:00:24 shubham
sometimes even the easy ones get you.. Wasted 1.5 hrs in this
2017-04-10 04:31:08
Nice Problem :D
Fermat's theorem and coin change.
2016-11-04 07:16:38
Wrong input constant
I fixed 0 < n < 10000 and 1<SP2(n)<10000 ---> AC
2016-06-26 14:51:52 surayans tiwari(http://bit.ly/1EPzcpv)
coin change :)
2016-06-24 18:47:46
Nyc qsn :) bottom up + precomputation
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.