hdoj Buy the Ticket 1133 (排列组合题)
2015-12-29 09:50
369 查看
Buy the Ticket
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5668 Accepted Submission(s): 2360
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
3 0
3 1
3 3
0 0
[align=left]Sample Output[/align]
Test #1:
6
Test #2:
18
Test #3:
180//题意:要去买票,每张票50元,但买票商没有多余的零钱。现在问有n个人是50元,m个人是100元,问总共有几种排列方式。//思路:转换:也就是从左往右遍历,并且50元的个数总大于等于100的个数,只有这样才符合要求。先找出符合要求的排列,也就是在m+n个空中最多填n个100,及C(n+m,n)再找出不符合要求的,也就是在m+n个空中填m+1个100,及C(n+m,m+1)所以共有( C(m+n, n) - C(m+n, m+1) ) * m! * n! 化简即 (m+n)! * (m-n+1) / (m+1) 种情况。
#include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> using namespace std; int a[1001]; void dsx(int n) { int c=0,i; for(i=0;i<=10000;i++) { c=a[i]*n+c; a[i]=c%10; c/=10; } } void dsz(int n) { int c=0,i; for(i=10000;i>=0;i--) { c=a[i]+c*10; a[i]=c/n; c%=n; } } int main() { int n,m,s,i,j,T=1; while(scanf("%d%d",&n,&m),n|m) { if(n<m) printf("Test #%d:\n0\n",T++); else { memset(a,0,sizeof(a)); a[0]=1; s=n+m; for(i=1;i<=s;i++) dsx(i); dsx(n-m+1); dsz(n+1); for(i=10000;i>=0;i--) if(a[i]) break; printf("Test #%d:\n",T++); for(j=i;j>=0;j--) printf("%d",a[j]); printf("\n"); } } }
相关文章推荐
- IOS倒计时的制作
- EASV8.1在英文sqlserver2012英文版数据库上连接的问题。
- android中listView单选
- 使用 Drools 规则引擎实现业务逻辑
- MySql常用命令
- Java基础学习总结(15)——java读取properties文件总结
- Java基础学习总结(15)——java读取properties文件总结
- Activity的四种启动模式和onNewIntent()
- sdk无法更新
- HDU_ACM_1001
- Java基础学习总结(15)——java读取properties文件总结
- db2命令行中执行sql
- 使用getGenericSuperclass()和getActualTypeArguments()将DAO做成泛型
- 对Android开发有用的技术栈(一)架构篇
- jblas、ujmp、jmatio的相互转化
- Android编程开发之TextView控件用法(2种方法)
- IT运维分析与海量日志搜索需要注意什么
- 在Linux和Window下如何查看一个端口是否监听以及被哪个进程监听命令
- Windows通用应用开发手记-Behavior SDK概述
- SAS学习笔记8