Caesar's Legions

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.

Examples

input1

2 1 1 10

output1

1

input2

2 3 1 2

output2

5

input3

2 4 1 1

output3

0

人话:有n1个步兵和n2个骑兵要排成一排,连续的步兵数量不能超过k1个,连续的骑兵数量不能超过k2个,问有几种排列方案

首先,这肯定是个dp

我一开始考虑用\(f[i][j]\)表示i个步兵,j个骑兵的方案数,然后发现列不出方程。。。

这时候,我们可以考虑加一维来使我们能列方程

\(f[i][j][0/1]\)表示i个步兵,j个骑兵,当前这一个是0:步兵;1:骑兵的方案数

这时候方程就很好列了,根据题意

\(f[i][j][0]\)只可能从\(f[i-k][j][1]\)转移,这时候\(0<k<k1\) ,

\(f[i][j][1]\)只可能从\(f[i][j-k][0]\)转移,这时候\(0<k<k2\),

这里的\(k\)表示的是连续的k个步兵或连续的k个骑兵

然后,Code:

#include <cstdio>
#include <cctype>
#include <algorithm>
#define reg register
using namespace std;
const int MaxN=101;
const int p=100000000;
template <class t> inline void rd(t &s)
{
s=0;
reg char c=getchar();
while(!isdigit(c))
c=getchar();
while(isdigit(c))
s=(s<<3)+(s<<1)+(c^48),c=getchar();
return;
}
int f[MaxN][MaxN][2];
signed main(void)
{
int n1,n2,k1,k2;
rd(n1);rd(n2);rd(k1);rd(k2);
for(int i=1;i<=k1;++i)
f[i][0][0]=1;
for(int i=1;i<=k2;++i)
f[0][i][1]=1;
for(int i=1;i<=n1;++i)
for(int j=1;j<=n2;++j)
{
for(int k=1;k<=min(i,k1);++k)
f[i][j][0]+=f[i-k][j][1],f[i][j][0]%=p;
for(int k=1;k<=min(j,k2);++k)
f[i][j][1]+=f[i][j-k][0],f[i][j][1]%=p;
// printf("%d %d\n",min(i,k1),min(j,k2));
// printf("%d %d\n",f[i][j][0],f[i][j][1]);
}
printf("%d",(f[n1][n2][0]+f[n1][n2][1])%p);
return 0;
}

有一点要注意的,for枚举k时要判一下,不能出现访问负数下标的情况,否则会RE或WA

<背包>solution-CF118D_Caesar's Legions的更多相关文章

  1. [HAOI2018]奇怪的背包 (DP,数论)

    [HAOI2018]奇怪的背包 \(solution:\) 首先,这一道题目的描述很像完全背包,但它所说的背包总重量是在模P意义下的,所以肯定会用到数论.我们先分析一下,每一个物品可以放无数次,可以达 ...

  2. 题解-HAOI2018全套

    去冬令营转了一圈发现自己比别人差根源在于刷题少,见过的套路少(>ω<) 于是闲来无事把历年省选题做了一些 链接放的都是洛谷的,bz偷懒放的也是链接 AM.T1 奇怪的背包 Problem ...

  3. Solution -「洛谷 P4389」付公主的背包

    \(\mathcal{Description}\)   Link.   容量为 \(n\),\(m\) 种物品的无限背包,求凑出每种容量的方案数,对 \(998244353\) 取模.   \(n,m ...

  4. 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 ...

  5. POJ1112 Team Them Up![二分图染色 补图 01背包]

    Team Them Up! Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7608   Accepted: 2041   S ...

  6. HDU 3033 分组背包变形(每种至少一个)

    I love sneakers! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. POJ1837 Balance[分组背包]

    Balance Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13717   Accepted: 8616 Descript ...

  8. vijos1431[noip2007]守望者的逃离(背包动规)

    描述 恶魔猎手尤迪安野心勃勃,他背叛了暗夜精灵,率领深藏在海底的娜迦族企图叛变.守望者 在与尤迪安的交锋中遭遇了围杀,被困在一个荒芜的大岛上.为了杀死守望者,尤迪安开始对这 个荒岛施咒,这座岛很快就会 ...

  9. 【BZOJ-2427】软件安装 Tarjan + 树形01背包

    2427: [HAOI2010]软件安装 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 960  Solved: 380[Submit][Status ...

随机推荐

  1. 最长无重复子串问题 leetcode 3

    一.代码及注释 class Solution { public: int lengthOfLongestSubstring(string s) { int n = s.size(); //字符串的长度 ...

  2. DEVOPS技术实践_19:Pipeline的多参数json调用

    在上一篇学习了把参数写进Json文件,然后通过去Json文件,调用参数的方法 1. 三元运算符介绍 调用的方法是通过一个三元运算符实现的 gender = prop.GENDER? prop.GEND ...

  3. C# 将PDF转为Word、Html、XPS、SVG、PCL、PS——基于Spire.Cloud.PDF

    Spire.Cloud.PDF提供了接口PdfConvertApi可用于将PDF文档转换为其他格式文档,如Word(docx/doc).Html.XPS.SVG.PCL.PS.Png以及XPS转成PD ...

  4. Python学习3月5号【python编程 从入门到实践】---》笔记(3)4

    1.字典 #####修改字典里面的KEYS数值和VALUES数值要用中括号# alien_0={'color':'green','point':5}# alien_0['color']='red'# ...

  5. 20191121-3 Final阶段贡献分配规则

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2019fall/homework/10063 ”组长”组final阶段贡献分分配规则 组里五位成员分别有入团队 ...

  6. 1063 计算谱半径 (20 分)C语言

    在数学中,矩阵的"谱半径"是指其特征值的模集合的上确界.换言之,对于给定的 n 个复数空间的特征值 { a1+b​1​​ i,⋯,a​n​​ +b​n​​ i },它们的模为实部与 ...

  7. Spring中常见的设计模式——模板模式

    一.模板模式的应用场景 模板模式又叫模板方法模式(Template Method Pattern),指定义一个算法的骨架,并允许自雷为一个或者多个步骤提供实现.模板模式使得子类可以在不改变算法结果的情 ...

  8. iOS从gif获取图片数组

    iOS中,当我们UIImageView实现动画时,如果图片是gif则不会自动播放gif图片,我们可以从gif图片中读取出每一帧的图片,然后组成图片数组,之后再实现使用UIImageView实现动画效果 ...

  9. 使用Java实现简单的Http服务器

    在Java中可以使用HttpServer类来实现Http服务器,该类位于com.sun.net包下(rt.jar).实现代码如下: 主程序类 package bg.httpserver; import ...

  10. infer 代码静态分析

    infer 代码静态分析 静态代码分析工具,主要是为了提高我们的代码质量. 通常,我们提高代码质量的方式是通过CodeReview,但是这个过程耗费的人工和时间往往较大.并且随着代码量的增加人肉检测起 ...