codeforces118D. Caesar's Legions
地址:http://www.codeforces.com/problemset/problem/118/D
题目:
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 beautifularrangements 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.
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.
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.
2 1 1 10
1
2 3 1 2
5
2 4 1 1
0
Let's mark a footman as 1, and a horseman as 2.
In the first sample the only beautiful line-up is: 121
In the second sample 5 beautiful line-ups exist: 12122, 12212, 21212, 21221, 22121
思路:好吧,这题又没自己做出来,看了别人的代码后才会的;
开始我用dp[i][0]和dp[i][1]来表示前i个人中以0或1结尾的排列方式有多少中,然后。。。。就没有然后了,写不下去了,因为前i个人中,我统计不出步兵和骑兵各有多少人。
。别人的做法:我只能说构思很巧!,算0结尾的用以1结尾的来计算!
使用状态dp【i】【j】【k】,i,j:步兵,骑兵个数 k:0是步兵,1是骑兵
状态方程:for(int k = 1; k <= min(i,k1); k++)
dp[i][j][0] = (dp[i][j][0] + dp[i-k][j][1])%MOD;
for(int k = 1; k <= min(j,k2); k++)
dp[i][j][1] = (dp[i][j][1] + dp[i][j-k][0])%MOD;
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <stack>
#include <map>
#include <vector> #define PI acos((double)-1)
#define E exp(double(1))
using namespace std; int dp[][][];
int main (void)
{
int n1,n2,k1,k2;
cin>>n1>>n2>>k1>>k2;
memset(dp,,sizeof(dp));
for(int i=;i<= min(n1,k1);i++)
dp[i][][] = ;
for(int i = ;i<=min(n2,k2);i++)
dp[][i][]=;
for(int i = ;i<=n1;i++)
for(int j = ;j<=n2;j++)
{
for(int k=;k<=min(i,k1);k++)
dp[i][j][]=(dp[i][j][]+dp[i-k][j][])%;
for(int k=;k<=min(j,k2);k++)
dp[i][j][]=(dp[i][j][]+dp[i][j-k][])%;
}
cout<<(dp[n1][n2][]+dp[n1][n2][])%<<endl;
return ;
}
codeforces118D. Caesar's Legions的更多相关文章
- Codeforces118D Caesar's Legions(DP)
题目 Source http://codeforces.com/problemset/problem/118/D Description Gaius Julius Caesar, a famous g ...
- Caesar's Legions(三维dp)
Caesar's Legions Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u S ...
- Caesar's Legions(CodeForces-118D) 【DP】
题目链接:https://vjudge.net/problem/CodeForces-118D 题意:有n1名步兵和n2名骑兵,现在要将他们排成一列,并且最多连续k1名步兵站在一起,最多连续k2名骑兵 ...
- Codeforces 118 D. Caesar's Legions (dp)
题目链接:http://codeforces.com/contest/118/problem/D 有n个步兵和m个骑兵要排成一排,其中连续的步兵不能超过k1个,连续的骑兵不能超过k2个. dp[i][ ...
- 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 ...
- 【dp】D. Caesar's Legions
https://www.bnuoj.com/v3/contest_show.php?cid=9146#problem/D [题意]给定n1个A,n2个B,排成一排,要求A最多能连续k1个紧挨着,B最多 ...
- 【Codeforces 118B】Caesar's Legions
[链接] 我是链接,点我呀:) [题意] 序列中不能连续出现k1个以上的1以及不能连续出现k2个以上的2,然后一共有n1个1以及n2和2,要求这n1+n2个数字都出现. 问序列有多少种可能. [题解] ...
- [CF118D]Caesar's Legions 题解
题意简述 一个01序列由\(n_1\)个0和\(n_2\)个1组成,求最长连续0串长度不超过\(k_1\),最长连续1串长度不超过\(k_2\)的序列的方案总数 题解 状态 方案总数 变量 已经取了i ...
- D. Caesar's Legions
\(状态很容易设计\) \(设dp[i][j][u][v]表示放了i个1兵种和j个2兵种\) \(然后u不会0说明末尾放了连续u个1兵种,v不为0说明末尾放了连续v个2兵种\) #include &l ...
随机推荐
- javascript中关于数组的一些鄙视题
一.判断一个数组中是否有相同的元素 /* * 判断数组中是否有相同的元素的代码 */ // 方案一 function isRepeat1(arrs) { if(arrs.length > 0) ...
- [js开源组件开发]html5标签audio的样式更改
html5标签audio的样式更改 由于html5的流行,现在移动端大多数的需求都可以使用audio来播放音频,但您可能只是需要很简单的播放/停止效果,但不同的浏览器上的audio样式却不尽人意,所以 ...
- Error: Error setting TTL index on collection : sessions
Error: Error setting TTL index on collection : sessions 一.步骤一: 这个问题一般是直接升级 mongodb和connect-mongo的版本为 ...
- JavaScript 左右上下自动晃动,自动移动。
最近做了一个项目,本来用css3动画做的,不兼容ie,用js做了个,分享给大家. 代码修改了下,上下左右四个模块,顺时针转动. <!DOCTYPE html> <html> & ...
- ng-cordova 手机拍照或从相册选择图片
1.需求描述 实现一个调用摄像头拍照,或者直接打开本地图库选择照片,然后替换App中图片的功能 2.准备 1) 安装ng-cordova 进入到ionic工程目录,使用bower工具安装, bower ...
- Flex Viewer (二)——体系结构
一.概述 在上一篇文章<深入浅出Flex Viewer (一)——概述>中,笔者对Flex Viewer用于构建以地图为中心的富客户端(RIA)应用的原型的功能和价值做了简要地介绍.在本文 ...
- sap后台作业管理/sap打印管理
sap后台作业管理: 后台作业,主要用于运行需要处理大量数据,对交互没有要求的程序.个人认为,简单的创建,配置和监控后台作业没有什么难度.后台作业管理最为困难的解决方案的取舍,系统负载的调控.失控的后 ...
- SharePoint 2013 操作文档库ECB菜单
在SharePoint的使用中,我们经常需要定制SharePoint的一系列菜单,这里就包括ECB菜单,下面,我们简单了解一下ECB菜单如何定制,以及原理. 1.正常情况文档库的ECB菜单如下图: 2 ...
- iOS 获取ssid
- (NSString *)fetchSSIDInfo { NSString *ssid = nil; NSArray *ifs = (__bridge_transfer id)CNCopySuppo ...
- [Android]使用RecyclerView替代ListView(一)
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/4232560.html RecyclerView是一个比List ...