题目

Source

http://codeforces.com/problemset/problem/118/D

Description

Gaius Julius Caesar, a famous general, loved to line up his soldiers. Overall the army had n1 footmen and n2 horsemen. Caesar thought that an arrangement is not beautiful if somewhere in the line there are strictly more that k1 footmen standing successively one after another, or there are strictly more than k2 horsemen standing successively one after another. Find the number of beautiful arrangements of the soldiers.

Note that all n1 + n2 warriors should be present at each arrangement. All footmen are considered indistinguishable among themselves. Similarly, all horsemen are considered indistinguishable among themselves.

Input

The only line contains four space-separated integers n1, n2, k1, k2 (1 ≤ n1, n2 ≤ 100, 1 ≤ k1, k2 ≤ 10) which represent how many footmen and horsemen there are and the largest acceptable number of footmen and horsemen standing in succession, correspondingly.

Output

Print the number of beautiful arrangements of the army modulo 100000000 (108). That is, print the number of such ways to line up the soldiers, that no more than k1 footmen stand successively, and no more than k2 horsemen stand successively.

Sample Input

2 1 1 10
2 3 1 2
2 4 1 1

Sample Output

1
5
0

分析

题目大概说有n1个步兵和n2骑兵要排成一排,连续步兵数不能超过k1个,连续骑兵数不能超过k2个,问有几种排列方案。

  • dp[i][j][x][y]表示已经有i个步兵j个骑兵参与排列且末尾有x个连续步兵或y个连续骑兵的方案数
  • 转移就是通过在末尾放上步兵或者骑兵转移

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int d[111][111][11][11];
int main(){
int n1,n2,k1,k2;
scanf("%d%d%d%d",&n1,&n2,&k1,&k2);
d[0][0][0][0]=1;
for(int i=0; i<=n1; ++i){
for(int j=0; j<=n2; ++j){
for(int x=0; x<=k1; ++x){
for(int y=0; y<=k2; ++y){
if(d[i][j][x][y]==0) continue;
if(i!=n1 && x!=k1){
d[i+1][j][x+1][0]+=d[i][j][x][y];
d[i+1][j][x+1][0]%=100000000;
}
if(j!=n2 && y!=k2){
d[i][j+1][0][y+1]+=d[i][j][x][y];
d[i][j+1][0][y+1]%=100000000;
}
}
}
}
}
int ans=0;
for(int i=1; i<=k1; ++i) ans+=d[n1][n2][i][0],ans%=100000000;
for(int i=1; i<=k2; ++i) ans+=d[n1][n2][0][i],ans%=100000000;
printf("%d",ans);
return 0;
}

Codeforces118D Caesar's Legions(DP)的更多相关文章

  1. LightOJ 1033 Generating Palindromes(dp)

    LightOJ 1033  Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  2. lightOJ 1047 Neighbor House (DP)

    lightOJ 1047   Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...

  3. UVA11125 - Arrange Some Marbles(dp)

    UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...

  4. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

  5. 初探动态规划(DP)

    学习qzz的命名,来写一篇关于动态规划(dp)的入门博客. 动态规划应该算是一个入门oier的坑,动态规划的抽象即神奇之处,让很多萌新 萌比. 写这篇博客的目标,就是想要用一些容易理解的方式,讲解入门 ...

  6. Tour(dp)

    Tour(dp) 给定平面上n(n<=1000)个点的坐标(按照x递增的顺序),各点x坐标不同,且均为正整数.请设计一条路线,从最左边的点出发,走到最右边的点后再返回,要求除了最左点和最右点之外 ...

  7. 2017百度之星资格赛 1003:度度熊与邪恶大魔王(DP)

    .navbar-nav > li.active > a { background-image: none; background-color: #058; } .navbar-invers ...

  8. Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)

    Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...

  9. 最长公共子序列长度(dp)

    /// 求两个字符串的最大公共子序列长度,最长公共子序列则并不要求连续,但要求前后顺序(dp) #include <bits/stdc++.h> using namespace std; ...

随机推荐

  1. Android开发笔记之《远程控制(MQTT|mosquitto) && (ProtocalBuffer | GRPC)》

    Android推送方案分析(MQTT/XMPP/GCM): http://www.open-open.com/lib/view/open1410848945601.htmlMQTT官网: http:/ ...

  2. Java创建对象的几种方法

    有时候,也可能碰到这样面试题,如: Java创建对象有哪几种方法? 除了new之外,java创建对象还有哪几种方式? 本文结合例子,给出几种Java创建对象的方法,Here we go~~~~ 使用n ...

  3. UITableView的添加、删除、移动操作

    #pragma mark -----表视图的移动操作----- //移动的第一步也是需要将表视图的编辑状态打开 //2.指定哪些行可以进行移动 - (BOOL)tableView:(UITableVi ...

  4. 3Struts2进阶----青软S2SH(笔记)

    关于上面这个红框里的问题,经过实际测试发现,struts2增加一个命名空间后,jsp页面里所引用的资源的路径,也需要增加一个"../", 于是,跟SpringMVC没啥区别了啊.. ...

  5. [nosql之redis]yum安装redis

    1.首先对于这种nosql来说目前我用到的功能很少,所以感觉没有必要去优化他跟不需要去编译安装.今天来介绍下一个yum安装redis 步骤1:安装扩展yum库 [root@localhost ~]# ...

  6. 跟随 Web 标准探究DOM -- Node 与 Element 的遍历

    写在前面 这篇没有什么 WebKit 代码的分析,因为……没啥好分析的,在实现里无非就是树的(先序DFS)遍历而已,囧哈哈哈……在WebCore/dom/Node.h , WebCore/dom/Co ...

  7. PHP 表单

    PHP 中的 $_GET 和 $_POST 变量用于检索表单中的信息,比如用户输入. 表单验证: 应该在任何可能的时候对用户输入进行验证(通过客户端脚本).浏览器验证速度更快,并且可以减轻服务器的负载 ...

  8. Sql Server 日期格式化函数

    Sql Server 中一个非常强大的日期格式化函数Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AMSelect CONVE ...

  9. 【荐】使用eval()、new Function()将JSON字符串转换为JSON对象

    在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式: 1.一种为使用eval()函数. 2. 使用Function对象来进行返回解析. 第一种解析方式:使用eval函数来解析,并且使用j ...

  10. 多线程更新UITableView时容易导致的问题

    我请求同一个接口两次, 第一次是那缓存, 第二次是那网络数据在请求成功回调的主线程异步的, 先赋值数据源, 然后调用uitableview reloaddata的方法, 这时候问题来了 reloadd ...