Buy the Ticket HDU - 1133 大数dp
题意:
演唱会门票售票处,那里最开始没有零钱。每一张门票是50元,人们只会拿着100元和50元去买票,有n个人是拿着50元买票,m个人拿着100元去买票。
n+m个人按照某个顺序按序买票,如果一个人拿着100元买票,而你没有零钱去找给他,那么买票结束。
题目问你,这n+m个人按照某个顺序按序买票,中间买票没有暂停的排队方式有多少种
题解:
我们设dp[i][j]表示一共有i个人,其中有j个人拿着50元买票的有效排队方式
说一下转移方程:
如果第i个人准备在前i-1个人的排队方式基础上拿着50元去买票,那么dp[i][j]要加上dp[i-1][j-1]
如果第i个人准备在前i-1个人的排队方式基础上拿着100元去买票,那么dp[i][j]要加上dp[i-1][j]
这里要说一下,我们每一个dp[i][j]都是最优的,而不是背包dp一样,中间状态不是最优
可能有人会想,你这个转移方程能够保存这么多排队方式嘛?看着不像呀!
我们这里实例来模拟一下:n=3,m=1
初始化:dp[0][0]=1
dp[1][1]=dp[0][0]+dp[0][1]=1,为了保证排队方式有效,那么前面拿着50元的人,肯定要大于等于100元的人,所以dp[1][0]就不存在
dp[2][1]=dp[1][0]+dp[1][1]=1。dp[2][2]=dp[1][1]+dp[1][2]=1
dp[3][1]=dp[2][0]+dp[2][1]=1。dp[3][2]=dp[2][1]+dp[2][2]=2 作为第一个值大于1的数,我们肯定要详细解释一下他的意思
dp[3][2]由dp[2][1]转化来这一部分,也就代表了50 100 50这个排队序列
dp[3][2]由dp[2][2]转化来这一部分,也就代表了50 50 50这个排队序列
剩下的dp状态都这样,你会发现,它们的状态其实并没有发生缺失情况,它只是以数字形式传递下去
因为这样算的时候我们把所有人都看作一样的,所以我们的答案需要乘于n的阶乘和m的阶乘,也就相当于按照人进行全排列A1n和A1m
代码:
#include<bits/stdc++.h>
#define MAXN 1000
using namespace std;
char fac[201][MAXN];
char dp[201][201][1005];
char ans1[100005],ans2[1000005];
int n,m;
void BigNumMultiSmall(char *a, char *b, int mul)
{
//a表示结果,b表示被乘数,mul表示乘数
int i, j, len;
int a_int[2000] = { 0 }, b_int[1000] = { 0 };
len = strlen(b);
for (i = 0; i < len; i++)
b_int[i] = b[len - 1 - i] - '0';
for (i = 0; i<len; i++)
{
a_int[i] = a_int[i] + b_int[i] * mul;
if (a_int[i]>9)
{
a_int[i + 1] = a_int[i] / 10;
a_int[i] = a_int[i] % 10;
}
}
while (a_int[i])
{
a_int[i + 1] = a_int[i] / 10;
a_int[i] = a_int[i] % 10;
i++;
}
while (a_int[i - 1] == 0)
i--;
for (j = 0; j < i; j++)
a[j] = a_int[i - j - 1] + '0';
a[j] = '\0';
}
void BigMultiBig(char *a, char *b, char *c)
{
int i, j, len1, len2, len;
int a_int[2010] = { 0 }, b_int[1000] = { 0 }, c_int[1000] = { 0 };
len1 = strlen(b);
for (i = len1 - 1; i >= 0; i--)
b_int[len1 - i - 1] = b[i] - '0';
len2 = strlen(c);
for (i = len2 - 1; i >= 0; i--)
c_int[len2 - i - 1] = c[i] - '0';
len = len1 + len2;
for (i = 0; i < len1; i++)
for (j = 0; j < len2; j++)
a_int[i + j] += b_int[i] * c_int[j];
for (i = 0; i<len; i++)
if (a_int[i]>9)
{
a_int[i + 1] += a_int[i] / 10;
a_int[i] = a_int[i] % 10;
}
while (a_int[len - 1] == 0)
len--;
for (i = 0; i < len; i++)
a[i] = a_int[len - i - 1] + '0';
a[i] = '\0';
if (strlen(a) == 0)
strcpy(a, "0");
}
void BigAddBig(char *a, char *b, char *c)
{
//a表示结果,b,c位加数
int a_int[1005] = { 0 }, b_int[1005] = { 0 }, c_int[1005] = { 0 };
int len1, len2, len, i;
len1 = strlen(b);
len2 = strlen(c);
for (i = 0; i < len1; i++)
b_int[i] = b[len1 - 1 - i] - '0';
for (i = 0; i<len2; i++)
c_int[i] = c[len2 - 1 - i] - '0';
len = len1>len2 ? len1 : len2;
for (i = 0; i<len; i++)
{
a_int[i] += b_int[i] + c_int[i];
if (a_int[i]>9)
{
a_int[i + 1] = a_int[i] / 10;
a_int[i] = a_int[i] % 10;
}
}
if (a_int[i] != 0)
len++;
while (!a_int[len - 1])
len--;
for (i = 0; i < len; i++)
a[i] = a_int[len - 1 - i] + '0';
a[i] = '\0';
}
void init() //求前缀乘
{
fac[0][0]='1';fac[1][0]='1';
for(int i=2;i<=200;i++)
{
BigNumMultiSmall(fac[i],fac[i-1],i);
// printf("%s\n",fac[i]);
}
}
void solve()
{
//dp[1][0][0]='1';
dp[0][0][0]='1';
//dp[0][0][1]='1';
for(int i=1;i<=210;i++) //一共有i个人
{
for(int j=1;j<=min(i,100);j++) //有j个人用50元
{
if(i-j>j)
{
continue;
}
BigAddBig(dp[i][j],dp[i-1][j-1],dp[i-1][j]); }
}
//printf("%s\n",dp[3][2]);
}
int main()
{
init();
solve();
int t=1;
while(scanf("%d %d",&n,&m))
{
if((!n)&&(!m))
{
break;
}
BigMultiBig(ans1,fac[n],dp[n+m][n]);
BigMultiBig(ans2,ans1,fac[m]);
printf("Test #%d:\n",t++);
printf("%s\n",ans2);
}
}
Buy the Ticket HDU - 1133 大数dp的更多相关文章
- Buy the Ticket HDU 1133 递推+大数
题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1133 题目大意: 有m+n个人去买电影票,每张电影票50元, m个人是只有50元一张的, n个人 ...
- Buy the Ticket HDU 1133 卡特兰数应用+Java大数
Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next ...
- Buy the Ticket HDU 1133
传送门 [http://acm.hdu.edu.cn/showproblem.php?pid=1133] 题目描述和分析 代码 #include<iostream> #include< ...
- 【hdoj_1133】Buy the Ticket(卡特兰数+大数)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1133 题目的意思是,m个人只有50元钱,n个人只有100元整钱,票价50元/人.现在售票厅没钱,只有50元 ...
- hdu 1502 大数dp
对于每一个dp的问题 从其最优解的结构(分哪几种形式或者情况)入手 然后分析状态 这样就比较好找出状态转方程这里数据结构的选择很简单 顺序数组就可以 填充的方式顺序填充就可以 然后这道题目卡了我大数. ...
- HDU 1133 Buy the Ticket (数学、大数阶乘)
Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- hdu 1133 Buy the Ticket (大数+递推)
Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- 【HDU 1133】 Buy the Ticket (卡特兰数)
Buy the Ticket Problem Description The "Harry Potter and the Goblet of Fire" will be on sh ...
- hdu 1133 Buy the Ticket(Catalan)
Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
随机推荐
- Centos 6 下安装 OSSEC-2.8.1 邮件告警 (二)
Ossec 配置邮件通知 ## 1 安装软件包: yum install -y sendmail mailx cyrus-sasl cyrus-sasl-plain #安装postfix邮件相关的软件 ...
- 【Linux】Linux系统dev/目录下的tty
终端是一种字符型设备,它有多种类型,通常使用tty来简称各种类型的终端设备.tty是Teletype的缩写.Teletype是最早出现的一种终端设备,很象电传打字机(或者说就是),是由Teletyp ...
- AWD生存之道
比赛开始阶段 常见漏洞的防御手段:https://www.freebuf.com/articles/web/208778.html 一.登陆SSH 重点 如果ssh的密码不是随机密码,记得一开始就进行 ...
- mysql InnoDB架构
1.InnoDB的磁盘结构 1)系统表空间 2)用户表空间 3)rodolog 文件组 4)磁盘文件逻辑结构 文件->段->区->页->行 InnoDB对数据的存取是以页为单位 ...
- salesforce零基础学习(一百)Mobile Device Tracking
本篇参考: Mobile Device Tracking (salesforce.com) UserDevice | SOAP API Developer Guide | Salesforce Dev ...
- c#使用谷歌身份验证GoogleAuthenticator
此功能相当于给系统加了个令牌,只有输入对的一组数字才可以验证成功.类似于QQ令牌一样. 一丶创建最核心的一个类GoogleAuthenticator 此类包含了生成密钥,验证,将绑定密钥转为二维码. ...
- GStreamer各个包构建
GStreamer按功能.维护的标准化程度.依赖库的版权差异等分了若干个包(package),如 gstreamer, gst-plugins-base, gst-plugins-good, gst- ...
- has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
前端显示: has been blocked by CORS policy: Response to preflight request doesn't pass access control che ...
- AES 密钥与 RSA 密钥的关系
AES 加密说明 - 支付宝开放平台 https://opendocs.alipay.com/open/common/104567 AES 密钥是对接口请求和响应内容进行加密,密文无法被第三方识别,从 ...
- 活动精彩实录 | 王峰:Cassandra在360的多场景应用及未来趋势
点击此处观看完整活动视频 大家好,我是360的王峰,我今天主要通过Cassandra在多场景下的应用来介绍一下Cassandra在360落地的情况. 我会从以下这几个方面进行介绍.首先介绍下Cassa ...