The Rascal Triangle 题解

来源:互联网 发布:linux route gateway 编辑:程序博客网 时间:2024/05/02 22:51

Description

Download as PDF

The Rascal Triangle definition is similar to that of the Pascal Triangle. The rows are numbered from the top starting with 0. Each rown contains n + 1 numbers indexed from 0 to n. Using R(nm) to indicate the index m item in the index n row:

R(nm) = 0 for n < 0 OR m < 0 OR m > n

The first and last numbers in each row (which are the same in the top row) are 1:

R(n, 0) = R(nn) = 1

The interior values are determined by (UpLeftEntry*UpRightEntry + 1)/UpEntry (see the parallelogram in the array below):

R(n + 1, m + 1) = (R(nm)*R(nm + 1) + 1)/R(n - 1, m)

\epsfbox{p5801.eps}

Write a program which computes R(nm) the m-th element of the n-th row of the Rascal Triangle.

Input

The first line of input contains a single integer P, ( 1$ \le$P$ \le$1000), which is the number of data sets that follow. Each data set is a single line of input consisting of 3 spaces separated decimal integers. The first integer is data set number, N. The second integer is row numbern, and the third integer is the index m within the row of the entry for which you are to find R(nm) the Rascal Triangle entry ( 0$ \le$m$ \le$n$ \le$50000).

Output

For each data set there is one line of output. It contains the data set number, N, followed by a single space which is then followed by theRascal Triangle entry R(nm) accurate to the nearest integer value.

Sample Input

51 4 02 4 23 45678 123454 12345 98765 34567 11398

Sample Output

1 12 53 4114958864 243838455 264080263

这一题不是像杨辉三家一样去递归,而是有公式:(n-m)*m+1

#include<stdio.h>#include<string.h>int T,i,N,n,m;int main(){    while(~scanf("%d",&T)){        for(i=0;i<T;i++){            scanf("%d %d %d",&N,&n,&m);            printf("%d %d\n",N,1+(n-m)*m);        }    }return 0;}