
The Rascal Triangle
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 243 Accepted Submission(s): 192
Problem Description
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(n,m) to indicte the index m
item in the index n row:
R(n,m) = 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(n,n) = 1
The interior values are determined by (UpLeftEntry*UpRightEntry+1)/UpEntry(see the parallelogram in the array below):
R(n+1, m+1) = (R(n,m) * R(n,m+1) + 1)/R(n-1,m)

Write a program which computes R(n,m) the
element of the
row of the Rascal Triangle.
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(n,m) to indicte the index m
item in the index n row:
R(n,m) = 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(n,n) = 1
The interior values are determined by (UpLeftEntry*UpRightEntry+1)/UpEntry(see the parallelogram in the array below):
R(n+1, m+1) = (R(n,m) * R(n,m+1) + 1)/R(n-1,m)

Write a program which computes R(n,m) the


Input
The
first line of input contains a single integer P, (1 <= P <=
1000), which is the number of data sets that follow. Each data set is a
single line of input consisting of 3 space separated decimal integers.
The first integer is data set number, N. The second integer is row
number n, and the third integer is the index m within the row of the
entry for which you are to find R(n,m) the Rascal Triangle entry (0
<= m <= n <= 50,000).
first line of input contains a single integer P, (1 <= P <=
1000), which is the number of data sets that follow. Each data set is a
single line of input consisting of 3 space separated decimal integers.
The first integer is data set number, N. The second integer is row
number n, and the third integer is the index m within the row of the
entry for which you are to find R(n,m) the Rascal Triangle entry (0
<= m <= n <= 50,000).
Output
For
each data set there is onr line of output. It contains the data set
number, N, followed by a single space which is then followed by thr
Rascal Triangle entry R(n,m) accurate to the integer value.
each data set there is onr line of output. It contains the data set
number, N, followed by a single space which is then followed by thr
Rascal Triangle entry R(n,m) accurate to the 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
Source
解析:利用题中所给递推公式,将数据存放到数组中有时候是个不错的选择。但本题这样做很可能会MLE,所以应该考虑找出通项公式。可以先写个小程序输出The Rascal Triangle的前几行,观察找规律。
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA0gAAAD2CAIAAABjrTAUAAAVxklEQVR4nO3dzXqrOLsG4Zz/SWsP+LYuGtALK3FQPUrdo152BhWBfpLY7q8mSZKkJXzNDpAkSdJneLCTJElaBOJg9/WFyJAkSYo2+UT19f/mZkiSJC0AcaLyYCdJkvRziBOVBztJkqSfQ5yoPNhJkiT9HOJE5cFOkiTp5xAnKg92kiRJP4c4UXmwkyRJ+jnEicqDnSRJ0s8hTlQe7CRJkn6O8gHFfkyxJEnSD3mWkiRJWoQHO0mSpEV4sJMkSVqEBztJkqRFeLCTJElaBOJgl/J+2JROSZL0N1E+7mRuxq2UTkmS9JchTiopB6aUTkmS9DchTiopB6aUTkmS9DchTiopB6aUTkmS9DchTiopB6aUTkmS9DchTiopB6aUTkmS9DchTiopB6aUTkmS9DchTiopB6aUTkmS9DchTiopB6aUTkmS9DdRPqAY/vG/KZ2SJOkv84wiSZK0CA92kiRJi/BgJ0mStAgPdpIkSYvwYCdJkrQIxMGO/z7TrLfERkRKkqSPo3zcydyM2iGPXBsxnpIk6ZcgTgDwg0jQwW7DL5QkSb8BcQKAH0Q82EmSpAiIE0DWQYRfyy+UJEm/AXECCDqIRKRGREqSpI9DnABSDiJ2SpIkMsQJIOIgEhG5CUqVJEkfhDgB8A8i/MK9rFpJkvQpiBMA/CDiu2IlSVIEygcUkz9W9+tkdtFQSqckSfoN7v2SJEmL8GAnSZK0CA92kiRJi/BgJ0mStAgPdpIkSYtAHOz4799MeatpSucmIlKSpCCUjzuZm1FL+Ry7lM4Wct0lSYqD2F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mpi3V4wYZzyLHlpkGyehIttd58SwvWLQUJ0tZDxb+P1Jm+9FDCf16+T81MS8vWLQOONZeB4J/QYkSZL0rzzYSZIkLcKDnSRJ0iI82EmSJC3Cg50kSdIiPNhJkiQtwoOdJEnSIjzYSZIkLeL/AMbnRKJBCNEbAAAAAElFTkSuQmCC" alt="" />
很容易发现通项公式为1+(n-m)*m。
#include <cstdio> int P;
int N,n,m; int main()
{
scanf("%d",&P);
while(P--){
scanf("%d%d%d",&N,&n,&m);
printf("%d %d\n",N,+(n-m)*m);
}
return ;
}