Faulhaber's Triangle

Time limit1sMemory limit128 MB

Summary
Build Faulhaber's triangle row by row using the recurrence F(i,j)=i/j*F(i-1,j-1) plus the normalization that each row sums to 1, then answer queries for F(m,k) as reduced fractions.
Level

Medium5 of 10

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

Problem

The sum of the mm-th powers of the integers from 11 to nn is

S(n,m)=∑j=1njm,S(n, m) = \sum_{j=1}^{n} j^m,

which can be written as a polynomial in nn of degree m+1m+1:

S(n,m)=∑k=1m+1F(m,k) nk.S(n, m) = \sum_{k=1}^{m+1} F(m, k)\, n^k.

For example,

S(n,1)=1+⋯+n=12n2+12nS(n, 1) = 1 + \dots + n = \tfrac{1}{2}n^2 + \tfrac{1}{2}n S(n,2)=1+⋯+n2=13n3+12n2+16nS(n, 2) = 1 + \dots + n^2 = \tfrac{1}{3}n^3 + \tfrac{1}{2}n^2 + \tfrac{1}{6}n S(n,3)=1+⋯+n3=14n4+12n3+14n2S(n, 3) = 1 + \dots + n^3 = \tfrac{1}{4}n^4 + \tfrac{1}{2}n^3 + \tfrac{1}{4}n^2 S(n,4)=1+⋯+n4=15n5+12n4+13n3−130nS(n, 4) = 1 + \dots + n^4 = \tfrac{1}{5}n^5 + \tfrac{1}{2}n^4 + \tfrac{1}{3}n^3 - \tfrac{1}{30}n

The coefficients F(m,k)F(m, k) form Faulhaber's triangle:

1
1/21/2
1/61/21/3
01/41/21/4
-1/3001/31/21/5
0-1/1205/121/21/6
1/420-1/601/21/21/7

In F(m,k)F(m, k), the row index mm is counted from the top starting at 00, and the column index kk is counted from the left starting at 11.

Faulhaber's triangle can be built as follows:

  1. For j>1j > 1, F(i,j)=ij F(i−1,j−1)F(i, j) = \dfrac{i}{j} \, F(i-1, j-1).
  2. F(i,1)F(i, 1) is chosen so that the entries in row ii add up to 11.

Given mm and kk, write a program that computes F(m,k)F(m, k).

Input

The first line contains the number of test cases PP (1≤P≤10001 \le P \le 1000). Each test case is a single line containing mm and kk separated by a space, with 0≤m≤4000 \le m \le 400 and 1≤k≤m+11 \le k \le m+1.

Output

For each test case, output F(m,k)F(m, k). If the value is an integer, print it as an integer; otherwise print it as a reduced fraction in the form p/qp/q.

Examples4

  1. Example 1

    Input
    4
    4 1
    4 3
    86 79
    400 401
    
    Expected output
    -1/30
    1/3
    -22388337
    1/401
    
  2. Example 2

    Input
    5
    4 1
    4 2
    4 3
    4 4
    4 5
    
    Expected output
    -1/30
    0
    1/3
    1/2
    1/5
    
  3. Example 3

    Input
    6
    0 1
    1 1
    1 2
    2 1
    2 2
    2 3
    
    Expected output
    1
    1/2
    1/2
    1/6
    1/2
    1/3
    
  4. Example 4

    Input
    4
    10 11
    10 10
    5 1
    6 1
    
    Expected output
    1/11
    1/2
    0
    1/42