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 ...
随机推荐
- 从客户端(?)中检测到有潜在危险的 Request.Path 值 的解决方案
public ActionResult A(string title) { return Redirect("B"+((String.IsNullOrEmpty(title))?& ...
- cell重用的几种方式
1.使用xib重用 //ios6 之后推荐大家使用的重用方式 //动态的使用self获得当前类名,来作为唯一的标示 NSString * identifier = NSStringFromClass( ...
- 基于SharePoint 2013的论坛解决方案[开源]
前言 这是自己在空闲时间里,为了提高对SharePoint的认识和熟悉技术,做的一个Demo.可能不尽完善,但是基本功能都已经有了,欢迎大家评论和提意见.自己也会在把源代码放到Github上进行开源, ...
- 【Leafletjs】1.创建一个地图
code: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <l ...
- Kotlin语法(函数和lambda表达式)
三.函数和lambda表达式 1. 函数声明 fun double(x: Int): Int { } 函数参数是用 Pascal 符号定义的 name:type.参数之间用逗号隔开,每个参数必须指明类 ...
- Failed to apply plugin [id 'com.android.application'] 和 Could not find com.android.tools.build:gradle:2.XX的最正确的解决方法
发现android studio是真的可爱啊,上一秒还没问题可以build运行,下一秒就出错...好,你任性,你牛逼.. 说下今天又遇到的两个问题:Failed to apply plugin [id ...
- 优化MySchool数据库(二)
优化School数据库(TSQL建库建表建约束) 使用T_sql代码建库.建表.建约束: 建库: Create database HotelManagerSystem on ( ---- 数据文件-- ...
- 【代码笔记】iOS-读取一段文字
一,效果图. 二,工程图. 三,代码. RootViewController.m #import "RootViewController.h" @interface RootVie ...
- NSURLConnection获取一个MP3文件
NSURLConnection网络请求 直接上代码-这个没什么说的,你们自己有兴趣可以自己试试 代码 #import "ViewController.h" @interface V ...
- SQL Server 2012实施与管理实战指南(笔记)——Ch6连接的建立和问题排查
6.连接的建立和问题排查 会话的建立分成2个部分: 1.连接,即找到这个实例 2.认证,告诉sql server谁要连接 目录 6.连接的建立和问题排查 6.1协议选择和别名 6.1.1 服务器网络配 ...