<背包>solution-CF118D_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的更多相关文章
- [HAOI2018]奇怪的背包 (DP,数论)
[HAOI2018]奇怪的背包 \(solution:\) 首先,这一道题目的描述很像完全背包,但它所说的背包总重量是在模P意义下的,所以肯定会用到数论.我们先分析一下,每一个物品可以放无数次,可以达 ...
- 题解-HAOI2018全套
去冬令营转了一圈发现自己比别人差根源在于刷题少,见过的套路少(>ω<) 于是闲来无事把历年省选题做了一些 链接放的都是洛谷的,bz偷懒放的也是链接 AM.T1 奇怪的背包 Problem ...
- Solution -「洛谷 P4389」付公主的背包
\(\mathcal{Description}\) Link. 容量为 \(n\),\(m\) 种物品的无限背包,求凑出每种容量的方案数,对 \(998244353\) 取模. \(n,m ...
- 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 ...
- POJ1112 Team Them Up![二分图染色 补图 01背包]
Team Them Up! Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7608 Accepted: 2041 S ...
- HDU 3033 分组背包变形(每种至少一个)
I love sneakers! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- POJ1837 Balance[分组背包]
Balance Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 13717 Accepted: 8616 Descript ...
- vijos1431[noip2007]守望者的逃离(背包动规)
描述 恶魔猎手尤迪安野心勃勃,他背叛了暗夜精灵,率领深藏在海底的娜迦族企图叛变.守望者 在与尤迪安的交锋中遭遇了围杀,被困在一个荒芜的大岛上.为了杀死守望者,尤迪安开始对这 个荒岛施咒,这座岛很快就会 ...
- 【BZOJ-2427】软件安装 Tarjan + 树形01背包
2427: [HAOI2010]软件安装 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 960 Solved: 380[Submit][Status ...
随机推荐
- nginx实现缓存功能
Nginx从0.7.48版本开始,支持了类似Squid的缓存功能.这个缓存是把URL及相关组合当作Key,用md5编码哈希后保存在硬盘上,所以它可以支持任意URL链接,同时也支持404/301/302 ...
- Autofac总结
Autofac 介绍|术语 控制反转:IOC和DI IOC 调用者不再创建(不自己new)被调用者的实例,而是交给容器去创建(AutoFac就充当这里的容器),这就是控制反转 控制反转中引入的第三方对 ...
- $POJ1015\ Jury\ Compromise\ Dp$/背包
洛谷传送门 $Sol$ 这是一道具有多个“体积维度”的$0/1$背包问题. 把$N$个候选人看做$N$个物品,那么每个物品有如下三种体积: 1.“人数”,每个候选人的“人数”都是$1$,最终要填满容积 ...
- TypeScript 源码详细解读(1)总览
TypeScript 由微软在 2012 年 10 月首发,经过几年的发展,已经成为国内外很多前端团队的首选编程语言.前端三大框架中的 Angular 和 Vue 3 也都改用了 TypeScript ...
- 【转】利用Eclipse编辑中文资源文件(application_zh_CN.properties )
既然生为中国人,就没有什么好抱怨的了,遇到编码的问题,那只有解决它了. 如果经常使用Struts,并做过国际化操作的人来说,对于中文资源文件的处理应该不会感到陌生的.比如下面两个文件,一个是英文的,一 ...
- TensorFlow——tf.contrib.layers库中的相关API
在TensorFlow中封装好了一个高级库,tf.contrib.layers库封装了很多的函数,使用这个高级库来开发将会提高效率,卷积函数使用tf.contrib.layers.conv2d,池化函 ...
- 最新IDEA永久激活攻略
前言 写这篇文章的原因是我最近想自己写两个项目,却发现自己的IDEA过期了,对,就是那个JAVA编辑器,于是研究了一下IDEA的激活.发现网上的攻略大多数不可用. 当然这里推荐大家去官网购买正版使用. ...
- 你的应用安全吗? ——用Xray和Synk保驾护航
一.背景 在当下软件应用的开发过程当中,自研的内部代码所占的比例逐步地减少,开源的框架和共用库已经得到了广泛的引用.如下图所示,在一个Kubernetes部署的应用当中,我们自己开发代码所占的比例可能 ...
- springboot2 + grpc
项目情况: springboot: 2.2.2 grpc-spring-boot-starter: 2.6.1.RELEASE 项目目录: mypro: - person - grpc服务端 - l ...
- acmPush模块示例demo
感谢论坛版主 马浩川 的分享. 模块介绍: 阿里移动推送(Alibaba Cloud Mobile Push)是基于大数据的移动智能推送服务,帮助App快速集成移动推送的功能,在实现高效.精确.实时 ...