HDU_4237_The Rascal Triangle(推公式)

The Rascal Triangle
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

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 row n 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

5
1 4 0
2 4 2
3 45678 12345
4 12345 9876
5 34567 11398

Sample Output

1 1
2 5
3 411495886
4 24383845
5 264080263

题型:找规律


题意:给出一个类似于杨辉三角的数塔,最左端和最右端都是1,递推式为

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

就是这个数等于其上面两个数的积再加1再除以其上面的上面的那个数,for example :(3*3+1)/ 2 = 5。

求出第n行第m个数是什么。


分析:根据递推式来写代码是不能的,会超时。直接求出一个式子直接用就可以了,代码及其简短。


代码:

#include<iostream>
#include<cstdio>
using namespace std;
int main(){
	int t,r;
	long long n,m,k;
	scanf("%d",&t);
	while(t--){
		scanf("%d%lld%lld",&r,&k,&m);	
		printf("%d %lld\n",r,1+(k-m)*m);
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值