GDUFE ACM-1020

The Rascal Triangle

Time Limit: 1000ms

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:

2011 Greater New York Regional 

这道题就是找规律:写成这样就可以发现规律了

AC代码:



 1 #include<bits/stdc++.h>
 2 using namespace std;
 3
 4 int main()
 5 {
 6     long long  n,m,k,b,j,c,i,t,a[50005];
 7     scanf("%lld",&t);
 8     while(t--)
 9     {
10         scanf("%lld%lld%lld",&k,&n,&m);
11         if(m==0||m==n)
12             printf("%lld 1\n",k);
13         else
14         {
15             b=n-3;
16             a[0]=1;
17             a[1]=n;
18             for(i=2;b>0;i++)
19             {
20                 a[i]=a[i-1]+b;
21                 b=b-2;
22             }
23             if(n%2==1)
24             {
25                 if(m<=n/2)
26                     printf("%lld %lld\n",k,a[m]);
27                 else
28                 {
29                     j=(n+1)/2;
30                     c=(m+1)%j;
31                     m=j-c;//后半部分,比如n=7时,a[4]=a[3],a[5]=a[2],a[6]=a[1];
32                     printf("%lld %lld\n",k,a[m]);
33                 }
34             }
35             else
36             {
37                 if(m<=n/2)
38                     printf("%lld %lld\n",k,a[m]);
39                 else
40                 {
41                     j=n/2;
42                     c=m%j;
43                     m=j-c;//类似上面
44                     printf("%lld %lld\n",k,a[m]);
45                 }
46             }
47         }
48     }
49     return 0;
50 }

另附上高人的代码(我没有想到这个思路):

 1 #include <stdio.h>
 2 //规律:(n-m)*m+1
 3 int main(){
 4     int t,N,n,m;
 5     scanf("%d",&t);
 6     while(t--){
 7         scanf("%d %d %d",&N,&n,&m);
 8         printf("%d %d\n",N,1+(n-m)*m);
 9     }
10     return 0;
11 }
时间: 2024-11-04 23:13:54

GDUFE ACM-1020的相关文章

http://acm.gdufe.edu.cn/Problem/read/id/1007

递推题目系列之一涂色问题 Time Limit: 2000/1000ms (Java/Others) Problem Description 有排成一行的n个方格,用红(Red).粉(Pink).绿(Green)三色涂每个格子,每格涂一色,要求任何相邻的方格不能同色,且首尾两格也不同色.求全部的满足要求的涂法. Input: 输入数据包含多个测试实例,每个测试实例占一行,由一个整数N组成,(0<n<=50). Output: 对于每个测试实例,请输出全部的满足要求的涂法,每个实例的输出占一行.

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY

北大ACM题库习题分类与简介(转载)

在百度文库上找到的,不知是哪位大牛整理的,真的很不错! zz题 目分类 Posted by fishhead at 2007-01-13 12:44:58.0 -------------------------------------------------------------------------------- acm.pku.edu.cn 1. 排序 1423, 1694, 1723, 1727, 1763, 1788, 1828, 1838, 1840, 2201, 2376, 23

GDUFE ACM-1356

题目:http://acm.gdufe.edu.cn/Problem/read/id/1356 Problem B. Relative atomic mass Time Limit: 2000/1000ms (Java/Others) Problem Description: Relative atomic mass is a dimensionless physical quantity, the ratio of the average mass of atoms of an element

GDUFE ACM-1087

题目:http://acm.gdufe.edu.cn/Problem/read/id/1087 辉辉的一天 Time Limit: 2000/1000ms (Java/Others) Problem Description: 辉辉.姗姗和佳佳是好朋友,他们一起参加了在湖南长沙长郡中学举办的第二十一届全国青少年信息学奥林匹克竞赛(NOI2004).他们很早就来到了长沙,可是报名还没有开始.怎么办呢?他们决定分头出去玩一天,晚上回到宿舍以后给大家说说自己这一天做了什么有意义的事情. 你一定想不到辉辉

GDUFE ACM-1050

题目:http://acm.gdufe.edu.cn/Problem/read/id/1050 a/b + c/d Time Limit: 2000/1000ms (Java/Others) Problem Description: 给你2个分数,求他们的和,并要求和为最简形式. Input: 输入首先包含一个正整数T(T<=1000),表示有T组测试数据,然后是T行数据,每行包含四个正整数a,b,c,d(0<a,b,c,d<1000),表示两个分数a/b 和 c/d. Output:

GDUFE ACM-1028 回文串

题目:http://acm.gdufe.edu.cn/Problem/read/id/1028 Problem Description: “回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串.请写一个程序判断读入的字符串是否是“回文”. Input: 输入包含多个测试实例,输入数据的第一行是一个正整数n,表示测试实例的个数,后面紧跟着是n个字符串. Output: 如果一个字符串是回文串,则输出"yes",否则输出"no". S

GDUFE ACM-1019 Repeating Characters

题目:http://acm.gdufe.edu.cn/Problem/read/id/1019 Problem Description: For this problem, you will write a program that takes a string of characters, S, and creates a new string of characters, T, with each character repaeated R times. That is, R copies

HDU字符串基础题(1020,1039,1062,1088,1161,1200,2017)

并不是很精简,随便改改A过了就没有再简化了. 1020. Problem Description Given a string containing only 'A' - 'Z', we could encode it using the following method: 1. Each sub-string containing k same characters should be encoded to "kX" where "X" is the only c

杭电ACM题目分类

杭电ACM题目分类 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028. 1029.1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092. 1093.1094.1095.1096.1097.1098.1106.1108.1157.1163.1164.1170.1194.1196. 1197.1201.1202.1205.1219.1234.123