标签:sig 复制 span 验证 output find tree ota exti
志远兄发现了一个神奇的递推公式,
某些递推的题目可以看作, 一个个上三角阵, 而问题的解为(1,1) 至 (n,n) 的路径个数, 废话不多说, 上题上代码
以下转自http://www.cnblogs.com/--ZHIYUAN/p/5971367.html
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9447 Accepted Submission(s): 4879
1 /*
2 原来不经过对角线是路径不穿过对角线的意思啊。递推打表,可以把棋盘下半部分遮住只看上半部分级列数大于等于行数的部分,这样每一个点(x,y)可以由(x-1,y),(x,y-1)推得
3 最后得到一半棋盘的结果再乘2。
4 */
5 #include<iostream>
6 #include<cstdio>
7 #include<cstring>
8 using namespace std;
9 long long a[40][40];
10 void init()
11 {
12 memset(a,0,sizeof(a));
13 for(int i=1;i<37;i++)
14 a[i][0]=1;
15 for(int i=1;i<37;i++)
16 {
17 for(int j=1;j<=i;j++)
18 {
19 a[i][j]=a[i-1][j]+a[i][j-1];
20 }
21 }
22 }
23 int main()
24 {
25 int t=0,n;
26 init();
27 while(scanf("%d",&n)&&n!=-1)
28 {
29 t++;
30 printf("%d %d %lld\n",t,n,a[n][n]*2);
31 }
32 return 0;
33 }
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3935 Accepted Submission(s): 2074
代码:
1 //见鬼了,这两道题的递推公式竟然一样。本来这道题想用dfs来搜但超时,然后发现数据结果竟和上一道题一样。可以这样想,
2 //设行H用列代表,D用行代表,列数要永远大于行数,这样又转换成了一个上三角的半个方格阵,求从求从起点到目标点的路径数。
3 #include<iostream>
4 #include<cstdio>
5 using namespace std;
6 int n,m;
7 long long a[21][21];
8 void init()
9 {
10 for(int i=1;i<=20;i++)
11 a[i][0]=1;
12 for(int i=1;i<=20;i++)
13 {
14 for(int j=1;j<=i;j++)
15 {
16 a[i][j]=a[i-1][j]+a[i][j-1];
17 }
18 }
19 }
20 int main()
21 {
22 init();
23 while(scanf("%d%d",&n,&m)!=EOF)
24 {
25 printf("%lld\n",a[n][m]);
26 }
27 return 0;
28 }
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3754 Accepted Submission(s): 2108
代码:
1 package luzhiyuan;
2 import java.util.Scanner;
3 import java.math.BigInteger;
4 public class java1 {
5 public static void main(String[] args){
6 BigInteger [][]a=new BigInteger[102][102];
7 BigInteger sta=BigInteger.valueOf(1); //把其他形式的数化为大整数
8 BigInteger zeo=BigInteger.valueOf(0);
9 for(int i=0;i<=100;i++)
10 for(int j=0;j<=100;j++)
11 a[i][j]=zeo; //如果想让后面的加法函数可用一定要给大整数赋初值
12 for(int i=1;i<=100;i++)
13 a[i][0]=sta;
14 for(int i=1;i<=100;i++)
15 for(int j=1;j<=i;j++){
16 a[i][j]=a[i][j].add(a[i-1][j]);
17 a[i][j]=a[i][j].add(a[i][j-1]);
18 }
19 Scanner cin=new Scanner(System.in);
20 while(cin.hasNext()){
21 int n=cin.nextInt();
22 System.out.println(a[n][n]);
23 }
24 }
25 }
还有一道题脑洞大开地运用了此递推方法:
1001: Buy the Ticket
Problem Description
The "Harry Potter and the Goblet of Fire" will be on show in the next few days. As a crazy fan of Harry Potter, you will go to the cinema and have the first sight, won’t you?
Suppose the cinema only has one ticket-office and the price for per-ticket is 50 dollars. The queue for buying the tickets is consisted of m + n persons (m persons each only has the 50-dollar bill and n persons each only has the 100-dollar bill).
Now the problem for you is to calculate the number of different ways of the queue that the buying process won‘t be stopped from the first person till the last person.
Note: initially the ticket-office has no money.
The buying process will be stopped on the occasion that the ticket-office has no 50-dollar bill but the first person of the queue only has the 100-dollar bill.
Input
The input file contains several test cases. Each test case is made up of two integer numbers: m and n. It is terminated by m = n = 0. Otherwise, m, n <=100.
Output
For each test case, first print the test number (counting from 1) in one line, then output the number of different ways in another line.
Sample Input
3 0
3 1
3 3
0 0
Sample Output
Test #1:
6
Test #2:
18
Test #3:
180
Source
HUANG, Ninghai
以为50元的数目要一直大于100元的数目, 这样就和下沙的沙子那道题一模一样了, 又见春哥...(逃....
标签:sig 复制 span 验证 output find tree ota exti
原文地址:http://www.cnblogs.com/liuzhanshan/p/6001975.html