<背包>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 ...
随机推荐
- 百度地图addEventListener“赋值”参数
实现点击百度地图上的覆盖物,然后获取覆盖上的属性,进而实现数据传送. var pointArray=new Array();//创建一个数组存储坐标 /*在地图上标点*/ function ShowA ...
- [工具] Git版本管理(三)(工作流)
一.冲突解决 Beyond Compare软件 下载BCompare软件,并安装. 删除安装目录下的BCUnrar.dll文件. 使用码: w4G-in5u3SH75RoB3VZIX8htiZgw4E ...
- FlyweightPattern(享元模式)-----Java/.Net
享元模式(Flyweight Pattern)主要用于减少创建对象的数量,以减少内存占用和提高性能.这种类型的设计模式属于结构型模式,它提供了减少对象数量从而改善应用所需的对象结构的方式
- HDU3709 Balanced Number 题解 数位DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3709 题目大意: 求区间 \([x, y]\) 范围内"平衡数"的数量. 所谓平衡 ...
- 机器学习实战笔记(一)- 使用SciKit-Learn做回归分析
一.简介 这次学习的书籍主要是Hands-on Machine Learning with Scikit-Learn and TensorFlow(豆瓣:https://book.douban.com ...
- nginx介绍及相关实验
一.nginx介绍 1.nginx简介 Nginx是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP 服务.Nginx 是由伊戈尔·赛索耶夫为俄罗斯访问量第二的 R ...
- git stash使用
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/daguanjia11/article/ ...
- 「newbee-mall新蜂商城开源啦」1000 Star Get !仓库Star数破千!记录一下
新蜂商城已经开源了 3 个多月左右的时间,在 2019 年的年末,仓库的 Star 数量冲破了 1000,整理本篇文章的时间是 2020 年 1 月 12 日,目前的 Star 数量是 1180 左右 ...
- 图解kubernetes调度器SchedulerCache核心源码实现
SchedulerCache是kubernetes scheduler中负责本地数据缓存的核心数据结构, 其实现了Cache接口,负责存储从apiserver获取的数据,提供给Scheduler调度器 ...
- 斯坦福算法分析和设计_2. 排序算法MergeSort
Motivate MergeSort是个相对古老的算法了,为什么现在我们还要讨论这么古老的东西呢?有几个原因: 它虽然年龄很大了,但是在实践中一直被沿用,仍然是很多程序库中的标准算法之一. 实现它 ...