<背包>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 ...
随机推荐
- 005 Ceph配置文件及用户管理
一.Ceph的配置文件 Ceph 配置文件可用于配置存储集群内的所有守护进程.或者某一类型的所有守护进程.要配置一系列守护进程,这些配置必须位于能收到配置的段落之下.默认情况下,无论是ceph的服务端 ...
- 急速搭建 Serverless AI 应用:为你写诗
前言 首先介绍下在本文出现的几个比较重要的概念: 函数计算(Function Compute): 函数计算是一个事件驱动的服务,通过函数计算,用户无需管理服务器等运行情况,只需编写代码并上传.函数计算 ...
- ansible安装与核心组件详解
第1章 安装anisble 1.1 安装epel源 rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarc ...
- 07Shell数组
Shell 数组变量 普通数组:只能使用整数作为数组索引 关联数组:可以使用字符串作为数组索引 普通数组 定义数组 方法1: 一次赋一个值 数组名[索引]=变量值 示例 # array1[0]=pea ...
- 手写vue observe数据劫持
实现代码: class Vue { constructor(options) { //缓存参数 this.$options = options; //需要监听的数据 this.$data = opti ...
- restapi-sql
身份验证,确定该成员是交过费的机构的成员,包含(用户名)和(密码) 各个表中的属性,有关timetemp等特殊类型,LocalDate等日期等具体格式. 引入了传输过程的不同的数据格式导致的两个错误, ...
- uni-app,vue,react,Trao之缓存类封装
uni-app,vue,react,Trao之缓存类封装 一,介绍与需求 1.1,介绍 缓存主要分为如下几个 1.LocalStorage LocalStorage是永久性的本地缓存,存储在客户端的浏 ...
- excel 转换成pdf 总结
excl 转换成pdf 1.freespire 只能转换前三页 // 使用此组件 只能转换前3页 //需要引用 如下命名空间 //using Spire.Doc; //Document doc = ...
- java架构之路(多线程)JUC并发编程之Semaphore信号量、CountDownLatch、CyclicBarrier栅栏、Executors线程池
上期回顾: 上次博客我们主要说了我们juc并发包下面的ReetrantLock的一些简单使用和底层的原理,是如何实现公平锁.非公平锁的.内部的双向链表到底是什么意思,prev和next到底是什么,为什 ...
- Mysql.linux登录数据库
//mysql -hlocalhost -uroot -p //-h数据库地址 -u用户名 -p密码 -P端口号(P大写)//-p可省略,会提示输入密码. mysql -h127. -uroot -p ...