CZ_PROB1 - Summing to a Square Prime

SP2={pp:prime(x1,x2Z,p=x21+x22)} is the set of all primes that can be represented as the sum of two squares. The function SP2(n) gives the nth prime number from the set SP2. Now, given two integers n (0<n<501) and k (0<k<4), find p(SP2(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<SP2(n)<7994
  • 0<k<4

Output

The p(SP2(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
2015-08-01 12:18:57 :.Mohib.:
Nice One..!!
2014-06-15 10:53:38 aravind katkuri
Nice one :)

Last edit: 2014-06-15 10:54:42
2013-11-30 11:40:56 Zachary Fakename
In case you solved the prime selection via *some known theorem*, notice that 2 = 1^2 + 1^2 is a sum-of-squares prime too, just not an odd one

Last edit: 2013-11-30 11:41:53
2013-10-31 17:51:45 Somesh Maurya™
@Nishant thanks buddy..dat was my 50th prob on spoj
2013-10-31 17:48:21 Somesh Maurya™
A hint for k=3 case :OEIS :-P
2013-10-31 12:03:25 Nishant Gupta
input contains some empty lines ....be careful while taking input !!
2013-08-25 18:07:29 siddharth saluja
nice problem :)
2013-05-17 20:11:42 Inspiron
3000B
2012-08-25 13:01:00 saket diwakar
nice one...
2012-07-20 09:42:36 Rishi Mukherje
Nice problem but pdf has the correct question. :). There are some empty lines in the input though.

Last edit: 2012-07-20 10:11:41
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.