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. 分布式全局唯一ID生成策略

    为什么分布式系统需要用到ID生成系统 在复杂分布式系统中,往往需要对大量的数据和消息进行唯一标识.如在美团点评的金融.支付.餐饮.酒店.猫眼电影等产品的系统中,数据日渐增长,对数据库的分库分表后需要有 ...

  2. Docker常用命令速查手册(华贵铂金版)

    原创声明:作者:Arnold.zhao  博客园地址:https://www.cnblogs.com/zh94 Docker常用命令速查手册 搜索仓库镜像 docker search nginx 获取 ...

  3. 【一起学源码-微服务】Ribbon 源码一:Ribbon概念理解及Demo调试

    前言 前情回顾 前面文章已经梳理清楚了Eureka相关的概念及源码,接下来开始研究下Ribbon的实现原理. 我们都知道Ribbon在spring cloud中担当负载均衡的角色, 当两个Eureka ...

  4. 我该如何学习spring源码以及解析bean定义的注册

    如何学习spring源码 前言 本文属于spring源码解析的系列文章之一,文章主要是介绍如何学习spring的源码,希望能够最大限度的帮助到有需要的人.文章总体难度不大,但比较繁重,学习时一定要耐住 ...

  5. 移动web 1像素边框

    实现方法 border-image 图片 实现 这篇文章是腾讯github上的解决方案border-image来实现的 链接走起 <使用border-image实现类似iOS7的1px底边> ...

  6. 如何对N个接口按比例压测

    随着微服务盛行,公司的服务端项目也越来越多.单一的接口性能测试并不能准确反映某个服务的总体处理能力,在服务功能划分比较清晰的架构下,对于某一服务的总体性能测试也相对变得简单.下面分享一个对于某个模块对 ...

  7. C#中的结构体和对象区别

    经常听到有朋友在讨论C#中的结构与类有什么区别.正好这几日闲来无事,自己总结一下,希望大家指点. 1. 首先是语法定义上的区别啦,这个就不用多说了.定义类使用关键字class 定义结构使用关键字str ...

  8. Spark 配置参数

    SparkConfiguration 这一章节来看看 Spark的相关配置. 并非仅仅能够应用于 SparkStreaming, 而是对于 Spark的各种类型都有支持. 各个不同. 其中中文参考链接 ...

  9. MySQL查询基础

    MySQL查询 DQL(Data Query Language ) 1.排序查询 # 语法: select 字段 from 表名 order by 字段1 [降序/升序],字段2 [降序/升序],.. ...

  10. 如何修改netbeans的系统字体?

    1. 打开/etc/netbeans.config 2. 找到netbeans_default_options 3. 追加 --fontsize 12