HDU 1133 Buy the Ticket 大数+卡兰特数的应用
来源:互联网 发布:单片机串口发送字符串 编辑:程序博客网 时间:2024/05/03 14:58
版权声明:本文为博主原创文章,未经博主允许不得转载。
原题目链接:点击打开链接
1、首先先考虑人无区别的情况
令f(m,n)表示有m个人手持50元的钞票,n个人手持100元的钞票时共有的方案总数。则可以分以下几种情况讨论:
(1)当n=0时
n=0意味着排队购票的所有人手中拿的都是50元的钞票,那么这m个人的排队方案总数为1.
(2)当m<n时
显然,f(m,n) = 0
(3)其他情况
考虑 (m+n) 个人排队购票的情景,第 (m+n) 个人站在第 (m+n-1) 个人的后面,则第 (m+n) 个人的排队方式可以
由下列两种情况获得:
a. 第 (m+n) 个人手持100元钞票,则在他之前的 (m+(n-1)) 个人手中有m 个人手持50元钞票,有 (n-1) 个人手持
100元钞票,此种情况共有 f(m,n-1);
b. 第 (m+n) 个人手持50元钞票,则在他之前的 ((m-1)+n) 个人手中有m-1 个人手持50元钞票,有 n 个人手持100
元钞票,此种情况共有 f(m-1,n);
根据加法原理得:f(m,n) = f(m,n-1) + f(m-1,n);
于是得到 f(m,n) 的计算公式:
可以推出如下公式:
f(m,n) = C(m+n,n)-C(m+n,m+1)
2、进一步考虑人有区别
可以推出:
f(m,n) = [C(m+n,n)-C(m+n,m+1)] * m! * n!
import java.math.*;import java.util.Scanner;public class Main{public static void main(String args[]){int n,m,count=1; BigInteger result = new BigInteger("0");Scanner cin = new Scanner(System.in);while(cin.hasNext()){m=cin.nextInt();n=cin.nextInt();if(m==0&&n==0)break;else{System.out.println("Test #"+count+":");if(n==0){result = factorial(m);}else if(m<n){result = BigInteger.valueOf(0);}else{result = factorial(m+n);result = result.multiply(BigInteger.valueOf(m+1-n));result = result.divide(BigInteger.valueOf(m+1));}System.out.println(result);count++;}}}public static BigInteger factorial(int n){BigInteger result = new BigInteger("1");for(int i=1;i <= n;i++){String temp1 = Integer.toString(i);BigInteger temp2 = new BigInteger(temp1);result = result.multiply(temp2);}return result;}}
1 0
- HDU 1133 Buy the Ticket 大数+卡兰特数的应用
- hdu1133 Buy the Ticket (卡兰特数应用+java大数)
- Buy the Ticket(卡特兰数应用与大数)
- hdu 1133 Buy the Ticket(Catalan数)
- HDU 1133 Buy the ticket catalan 数
- HDU 1133 Buy the Ticket 50,100钱买票,大数乘除法,卡特兰数变形
- hdu 1133 Buy the Ticket(卡特兰数变形+组合数学+java大数)
- HDOJ/HDU 1133 Buy the Ticket(数论~卡特兰数~大数~)
- hdu 1133 · 【Buy the Ticket】Catalan数+java大数类
- hdu 1133(大数——Buy the Ticket)
- HDU 1133 Buy the Ticket DP+java大数
- hdu 1133 Buy the Ticket
- buy the ticket hdu 1133
- HDU 1133 Buy the Ticket
- hdu 1133 Buy the Ticket
- hdu-1133-Buy the Ticket
- HDU-1133-Buy the Ticket
- HDU 1133 Buy the Ticket
- 基于Nutch+Hadoop+Hbase+ElasticSearch的网络爬虫及搜索引擎
- iOS-即时通信
- leetcode之search for a range
- 数据结构(1):顺序表
- 使用APMServ配置zend framework时出现open_basedir restriction in effect问题
- HDU 1133 Buy the Ticket 大数+卡兰特数的应用
- [LeetCode]121. Best Time to Buy and Sell Stock
- Android GridView 实现9宫格菜单,并实现item点击按压效果更改图片
- Linux下套接字详解(六)----基于pthread的多线程的TCP套接字(阻塞/同步/并发)
- Sql去重及查询重复
- 带参数的构造函数
- 7.12 编程练习
- Problem A: 写一个函数,使给定的一个二维数组(3×3)转置,即行列互换
- DSP 2812: 使用C++封装SCI