Caesar's Legions(三维dp)
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
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
1
2 3 1 2
5
2 4 1 1
0
Hint
Let's mark a footman as 1, and a horseman as 2.
In the first sample the only beautiful line-up is: 121
In the second sample 5 beautiful line-ups exist: 12122, 12212, 21212, 21221, 22121
The problem is solved lazy dynamics. Let z[n1] [n2] [2] - a number of ways to place troops in a legion of Caesar. Indicate the following parameters, n1 – is a number of footmen, n2 – is a number of horseman, the third parameter indicates what troops put Caesar in the beginning of the line. If Caesar wants to put the footmen, the state dynamics of the z [n1] [n2] [0] go to the state
z [n1 - i] [n2] [0], where 0 <= i <= min (k1, n1) . If Caesar wants to put the riders, the state dynamics of the z [n1] [n2] [1] go to the state z [n1] [n2 - i] [1], where 0 <= I <= min (k2, n2) .
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int mod = ;
int n1 , n2 , k1 , k2 ;
int a[][][] ; int Caesar (int n1 , int n2 , int f)
{
if (a[n1][n2][f] != - ) {
return a[n1][n2][f] ;
}
if (n1 + n2 == ) {
a[n1][n2][f] = % mod ;
return a[n1][n2][f] ;
}
a[n1][n2][f] = ;
int i ;
if (f == ) {
for (i = ; i <= min (k1 , n1 ) ; i++) {
a[n1][n2][f] += Caesar (n1 - i , n2 , - f) ;
a[n1][n2][f] %= mod ;
}
}
else {
for (i = ; i <= min (k2 , n2 ) ; i++) {
a[n1][n2][f] += Caesar (n1 , n2 - i , - f ) ;
a[n1][n2][f] %= mod ;
}
}
return a[n1][n2][f] ;
} void solve ()
{
memset (a , 0xFF , sizeof(a) ) ;
printf ("%d\n" , ( Caesar (n1 , n2 , ) + Caesar (n1 , n2 , ) ) % mod ) ;
} int main ()
{
#ifdef online_jude
freopen ("a.txt" , "r" , stdin ) ;
#endif // online_jude
scanf ("%d%d%d%d" , &n1 , &n2 , &k1 , &k2 ) ;
solve () ;
return ;
}
Caesar's Legions(三维dp)的更多相关文章
- Codeforces118D Caesar's Legions(DP)
题目 Source http://codeforces.com/problemset/problem/118/D Description Gaius Julius Caesar, a famous g ...
- D. Caesar's Legions 背包Dp 递推DP
http://codeforces.com/problemset/problem/118/D 设dp[i][j][k1][k2] 表示,放了i个1,放了j个2,而且1的连续个数是k1,2的连续个数是k ...
- 三维dp&codeforce 369_2_C
三维dp&codeforce 369_2_C 标签: dp codeforce 369_2_C 题意: 一排树,初始的时候有的有颜色,有的没有颜色,现在给没有颜色的树染色,给出n课树,用m种燃 ...
- P1006 传纸条(二维、三维dp)
P1006 传纸条 输入输出样例 输入 #1 复制 3 3 0 3 9 2 8 5 5 7 0 输出 #1 复制 34 说明/提示 [限制] 对于 30% 的数据,1≤m,n≤10: 对于 100% ...
- Codeforces 118 D. Caesar's Legions (dp)
题目链接:http://codeforces.com/contest/118/problem/D 有n个步兵和m个骑兵要排成一排,其中连续的步兵不能超过k1个,连续的骑兵不能超过k2个. dp[i][ ...
- 【dp】D. Caesar's Legions
https://www.bnuoj.com/v3/contest_show.php?cid=9146#problem/D [题意]给定n1个A,n2个B,排成一排,要求A最多能连续k1个紧挨着,B最多 ...
- Caesar's Legions(CodeForces-118D) 【DP】
题目链接:https://vjudge.net/problem/CodeForces-118D 题意:有n1名步兵和n2名骑兵,现在要将他们排成一列,并且最多连续k1名步兵站在一起,最多连续k2名骑兵 ...
- dp D. Caesar's Legions
https://codeforces.com/problemset/problem/118/D 这个题目有点思路,转移方程写错了. 这个题目看到数据范围之后发现很好dp, dp[i][j][k1][k ...
- codeforces118D. Caesar's Legions
地址:http://www.codeforces.com/problemset/problem/118/D 题目: Gaius Julius Caesar, a famous general, lov ...
随机推荐
- 20145215《Java程序设计》第6周学习总结
20145215<Java程序设计>第六周学习总结 教材学习内容总结 输入/输出 InputStream与OutputStream 从应用程序角度来看,如果要将数据从来源取出,可以使用输入 ...
- windows编程原理
这里在学网络编程时遇到了讲解windows的编程,稍微整理一下windows编程原理,顺便复习一下. 首先,理解Windows 程序运行原理:Windows应用程序,操作系统,计算机硬件之间的相互关系 ...
- 学习笔记——Maven实战(七)常用Maven插件介绍(上)
我们都知道Maven本质上是一个插件框架,它的核心并不执行任何具体的构建任务,所有这些任务都交给插件来完成,例如编译源代码是由maven-compiler-plugin完成的.进一步说,每个任务对应了 ...
- windows2003 单网卡 搭建vpn ,windows 2008 R2 类似吧。网上转的,自己加了点经验总结
先说说可能的坑,也就是我自己搭建的时候碰到的问题. 其实搭建起来很简单,我不知道我总结的对还是不对,但是按下面这样操作做绝对能搭好!因为我就是按这样搭起来的,但是我发现,好像pptp已经被墙了,我也不 ...
- jquery hasClass、removeClass、addClass方法
hasClass(class) 检查当前的元素是否含有某个特定的类,如果有,则返回true. 参数: class:用于匹配的类名. ---------------------------------- ...
- 《TCP/IP详解卷1:协议》第3章 IP:网际协议(1)-读书笔记
章节回顾: <TCP/IP详解卷1:协议>第1章 概述-读书笔记 <TCP/IP详解卷1:协议>第2章 链路层-读书笔记 <TCP/IP详解卷1:协议>第3章 IP ...
- 页面无法访问 css文件加载问题
1.青奥项目的web配置: 后缀为.html和.vm的请求会被控制器拦截. 控制器没设置目标资源,所以无法访问到资源! 2.文件不能放在vm文件夹下,因为设置了视图解析,vm文件夹下的文件只有后缀为 ...
- firefox与chrome中对select下拉框中的option支持问题
firefox可以直接修改option的字体样式,但是chrome中option的字体样式是继承select的,这个是在项目中遇到的,具体的可以看一下 http://www.cnblogs.com/r ...
- 【poj2187】 Beauty Contest
http://poj.org/problem?id=2187 (题目链接) 题意 求点集上两点间最长距离 Solution 凸包+旋转卡壳. 旋转卡壳是看起来很难,但是很好意会也很好实现的算法,但是要 ...
- 获取本机IP_考虑多网卡的情况
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...