HDUOJ---1133(卡特兰数扩展)Buy the Ticket
Buy the Ticket
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3614 Accepted Submission(s): 1522
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
Author
HUANG, Ninghai
Recommend
Eddy
http://acm.hdu.edu.cn/showproblem.php?pid=1133
将卡特兰扩展中的排队买票及相关问题都OVER了!!
解题思路(转):( C(m+n, n) - C(m+n, m+1) ) * m! * n! 化简即 (m+n)! * (m-n+1) / (m+1) 推導過程如下:
m個人拿50,n個人拿100
1、如果n > m,那麼排序方法數為0,這一點很容易想清楚
2、現在我們假設拿50的人用‘0’表示,拿100的人用‘1’表示。
如果有這麼一個序列0101101001001111。
當第K個位置出現1的個數多餘0的個數時就是一個不合法的序列了
假設m=4,n=3的一個序列是:0110100 。顯然,它不合法,現在我們把它稍微變化一下:
把第二個1(這個1前面的都是合法的)後面的所有位0變成1,1變成0.
就得到0111011這個序列1的數量多餘0的數量,顯然不合法,但現在的關鍵不是看這個序列是不是合法的
關鍵是:他和我們的不合法序列0110100成一一對應的關係。
也就是說任意一個不合法序列(m個0,n個1),都可以由另外一個序列(n-1個0和m+1個1)得到。
另外我們知道,一个序列要麼是合法的,要麼是不合法的
所以,合法序列數量 = 序列總數量 - 不合法序列的總量
序列總數可以這樣計算 m+n個位置中,選擇n個位置出來填上1,所以是C(m+n,n).
不合法序列的數量就是: m+n個位置中,選擇m+1個位置出來填上1,所以是C(m+n,m+1).
然後每個人都是不一樣的,所以需要全排列m! * n!.
所以最後的公式為:( C(m+n,n) - C(m+n,m+1) ) * m! * n!
化簡即為:(m+n)!*(m-n+1)/(m+1)
推廣:
如果原來有p張50元的話,那麼不合法的序列的數量應該是:任意一個不合法序列(m個0,n個1),都可以由另外一個序列(n-1個0和m+1+p個1)得到,所以是m+n個位置中,選擇m+1+p個位置,出來填上1所以是C(m+n,m+1+p),接下來簡化就不推了
代码:
1 #include<iostream>
2 #include<cstring>
3 #define maxn 500
4 using namespace std;
5 int arr[maxn+1];
6 int main()
7 {
8 int m,n,time=1,i,c,j,s,k; //m stand for person has 50 yuan, n stand for 100 yuan
9 while(cin>>m>>n,m+n)
10 {
11 memset(arr,0,sizeof arr);
12 cout<<"Test #"<<time++<<":"<<endl;
13 if(m<n)
14 {
15 cout<<0<<endl;
16 continue;
17 }
18 arr[0]=1;
19 for(i=2,c=0;i<=m+n;i++) //(m+n)!
20 {
21 for(j=0;j<=maxn;j++)
22 {
23 s=arr[j]*i+c;
24 arr[j]=s%10;
25 c=(s-arr[j])/10;
26 }
27 }
28 for(c=j=0;j<=maxn;j++) //(m+n)!*(m+1-n)
29 {
30 s=arr[j]*(m+1-n)+c;
31 arr[j]=s%10;
32 c=(s-arr[j])/10;
33 }
34 for(k=maxn;arr[k]==0;k--);
35 for(c=0,j=k;j>=0;j--) //(m+n)!*(m+1-n)/(m+1)
36 {
37 s=(arr[j]+10*c);
38 c=s%(m+1);
39 arr[j]=(s-c)/(m+1);
40 }
41 for(k=maxn;arr[k]==0;k--);
42 for(j=k;j>=0;j--)
43 cout<<arr[j];
44 cout<<endl;
45 }
46 return 0;
47 }
- 仿淘宝收货地址,本地数据库
- 小程序-实现竖排文字
- 【深度学习量化投资】RNNs在股票价格预测的应用基于Keras
- 关于webview调用js出现has no method 'toString'
- 深入学习Apache Spark和TensorFlow
- 搭建 WPF 上的 UI 自动化测试框架
- ttf设置文字字体
- R语言构建追涨杀跌量化交易模型(附源代码)
- Apache Spark中使用DataFrame的统计和数学函数
- android进程 清理及activity栈管理
- 机器学习模型的变量评估和选择基于技术指标『深度解析』
- Picasso and Android-Universal-Image-Loader缓存框架
- 解决ListView嵌套ListView遇到的问题
- 《OEA - 实体扩展属性系统 - 设计方案说明书》
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Android仿京东首页秒杀倒计时
- 超实用的android自定义log日志输出工具类
- spring进行mock测试
- RecyclerView实现纵向和横向滚动
- Android ListView列表视图的使用方法
- 滴滴自动化运维平台夜莺实战部署
- Android UI使用HorizontalListView实现水平滑动
- python操作yaml说明
- python由已知数组快速生成新数组的方法
- Git 修改已提交 commit 的信息
- 解决windows下python3使用multiprocessing.Pool出现的问题
- 解决pyqt5异常退出无提示信息的问题
- TensorFlow2.1.0安装过程中setuptools、wrapt等相关错误指南
- 来来来,让咱重新认识一下算法的复杂度!
- 带你通过字节跳动面试---操作系统复习