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.
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).
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.
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+jnEicqDnSRJ0s8hTlQe7CRJkn6O8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/wKRMpZWHqRj2oyFZu8Ch15/TsYpo/+bL3wcezC70/94+/H3OWsn6m7EcdfH+/5cHum4rZTkiNm0iby2xOc9HDiWxXnai87rZzenbWwY4/npvc+/PwFA12/Yzbj+D7+y0Pdt90+bPR6Knp9m3Pf1X7Mvjkr3sgkW3Q2ZckzqUvOkf/fN9ovtA6W8h4tvD78/wsB3z93OPvR1n7+5kHu28KuvCjhZ7TeTmxaeOZstCPNk7apb89iBCW+4dzZ3pnCxnPFn5/np8liFg/O/5+1KL290se7L6pWNlRqXUMNpU2nrc9hMiWcxCp2/pGNb3zYLS4T+9MGc8F7s/iwbnI62eXsh+l7O8jHuy+abR67s1q21f98Atett84UeN52wMZyVEnrfbh9Z3eebC/Py8fnyVlPJe5P6cXXvqirp+96odf8Jrz5QaOZ+FJHuIboI1j0QNJfbKyT0990jM98iAisrF/I7JHnkqjcXM8fy76/oQUBq2fEftRlzKJRjzYfRP8wj/ZkMidtw9OFBHZcv6a8PDwNMWTwwehcy+98/zUXOT5nrJ+puxHHXx/vxVwsKP9/rPo4aR+nZyfmpi3V4wYZzyLHlpkGyehIttd58SwvWLQUJ0tZDxb+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 ;
}
- hdu 1451 Area in Triangle(计算几何 三角形)
Given a triangle field and a rope of a certain length (Figure-1), you are required to use the rope t ...
- HDU 4324 (拓扑排序) Triangle LOVE
因为题目说了,两个人之间总有一个人喜欢另一个人,而且不会有两个人互相喜欢.所以只要所给的图中有一个环,那么一定存在一个三元环. 所以用拓扑排序判断一下图中是否有环就行了. #include <c ...
- 【2011 Greater New York Regional 】Problem B The Rascal Triangle
一个简单的规律题,每一列都是一个等差数列: 代码: #include<cstdio> #define ll long long using namespace std; int main( ...
- HDU 5914 Triangle 数学找规律
Triangle 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5914 Description Mr. Frog has n sticks, who ...
- hdu 4324 Triangle LOVE
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4324 Triangle LOVE Description Recently, scientists f ...
- HDU 5914 Triangle 【构造】 (2016中国大学生程序设计竞赛(长春))
Triangle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- HDU 5914 Triangle(打表——斐波那契数的应用)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5914 Problem Description Mr. Frog has n sticks, whos ...
- HDU 2018 Multi-University Training Contest 1 Triangle Partition 【YY】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6300 Triangle Partition Time Limit: 2000/1000 MS (Java ...
- hdu 3304 Interesting Yang Yui Triangle
hdu 3304 Interesting Yang Yui Triangle 题意: 给出P,N,问第N行的斐波那契数模P不等于0的有多少个? 限制: P < 1000,N <= 10^9 ...
随机推荐
- uc/os 任务删除
问题描述: uc/os 任务删除 问题解决: uc/os任务删除流程图 具体代码 注: 如上是关中断,以及取消优先级对应的就绪标志 关中断代码为: 取消就绪标志,实际上是将就绪表中指定 ...
- HDU4602+推导公式
手动列出前5项 可发现规律 /* 推导公式 a[n] = 2^(n-1) + (n-2)*2^(n-3) */ #include<stdio.h> #include<math.h&g ...
- linux查看在线用户 who命令参数及用法
linux who 命令 详解 Linux最常用命令之一 功能说明:显示目前登入系统的用户信息. 语 法:who [-Himqsw][--help][--version][am i][记录文件] 补充 ...
- 在RedHat5.4 LINUX 安装mySQL数据库
linux下mysql 最新版安装图解教程 1. 查看当前安装的linux版本 通过上图中的数据可以看出安装的版本为RedHat5.4,所以我们需要下载RedHat5.4对应的mysql安装包
- 阿里云 EDAS-HSF 用户指南
阿里云 EDAS-HSF 用户指南 针对 EDAS v2.3.0©Alibaba EDAS 项目组2015/8/19 1 前言本文档旨在描述阿里云 EDAS 产品中应用服务化模块的基本概念,以及如何使 ...
- 易企秀 we+ Maka 兔展 四大H5页面制作工具
H5这个由HTML5简化而来的词汇,正通过微信广泛传播.H5是集文字.图片.音乐.视频.链接等多种形式的展示页面,丰富的控件.灵活的动画特效.强大的交互应用和数据分析,高速低价的实现信息传播,非常适合 ...
- 201. Bitwise AND of Numbers Range
题目: Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all num ...
- SQL Server中的分页
sqlserver2000时的分页思路 .分页查询时,首先将数据排序 select * from MyStudent order by fid desc .取第一页数据 * from MyStuden ...
- P154、面试题28:字符串的排列
题目:输入一个字符串,打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a.b.c所能排列出来的所有字符串abc.acb.bac.bca.cab.cba. 测试用例: 1)功能测试( ...
- P38、面试题3:二维数组中的查找
题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 首先选取数组中右上角的数字 ...