codeforces118D - Caesar's Legions 多维DP

题意:给你n1个人,n2匹马站成一排,最多k1个人连续站,最多k2匹马连续站,问你有多少种方法

解题思路:4维dp,i,j,s,k分别代表位置,已经站了多少人,前一个站的是人还是马,一共连续站了几位了。

解题代码:

 1 // File Name: 118d.cpp
 2 // Author: darkdream
 3 // Created Time: 2014年07月25日 星期五 15时35分03秒
 4
 5 #include<vector>
 6 #include<list>
 7 #include<map>
 8 #include<set>
 9 #include<deque>
10 #include<stack>
11 #include<bitset>
12 #include<algorithm>
13 #include<functional>
14 #include<numeric>
15 #include<utility>
16 #include<sstream>
17 #include<iostream>
18 #include<iomanip>
19 #include<cstdio>
20 #include<cmath>
21 #include<cstdlib>
22 #include<cstring>
23 #include<ctime>
24 #define LL long long
25 const int  M = 100000000;
26 using namespace std;
27 int dp[204][200][3][20];
28 int main(){
29     int n1,n2,k1,k2;
30     scanf("%d %d %d %d",&n1,&n2,&k1,&k2);
31     memset(dp,0,sizeof(dp));
32     dp[1][1][0][1] = 1;
33     dp[1][0][1][1] = 1;
34
35     for(int i = 2;i <= n1+n2;i ++){
36         for(int j = 0 ;j <= n1;j ++){
37            for (int s = 0;s <= 1; s ++){
38                  if((s && j == n1+1) ||(!s && i-j == n2+1))
39                      continue;
40                  for(int k = 1;k <= (s?k2:k1);k ++){
41                        if(k == 1){
42                          for(int t = 1;t <= 10 ; t++)
43                          {
44                             dp[i][j][s][1] = (dp[i][j][s][1] + dp[i-1][(s?j:j-1)][!s][t] )%M;
45                          }
46                        }else{
47                             dp[i][j][s][k] = dp[i-1][(s?j:j-1)][s][k-1];
48                        }
49             //           printf("%d %d %d %d %d\n",i,j,s,k,dp[i][j][s][k]);
50                   }
51            }
52         }
53     }
54     LL sum = 0 ;
55
56     for(int s = 0;s <= 1;s ++)
57     {
58         for(int i = 1;i <= 10 ;i ++)
59         {
60           sum = (sum +dp[n1+n2][n1][s][i])%M;
61         }
62     }
63     printf("%I64d\n",sum);
64 return 0;
65 }

codeforces118D - Caesar's Legions 多维DP

时间: 2025-01-16 11:34:42

codeforces118D - Caesar's Legions 多维DP的相关文章

Codeforces118D Caesar&#39;s Legions(DP)

题目 Source http://codeforces.com/problemset/problem/118/D Description 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 somewhe

Codeforces-118D. Caesar&#39;s Legions(lazy dynamics)

传送门 把n1个步兵和n2个骑兵派成一列,已知连续的步兵不超过k1个,连续的骑兵不超过k2个,求总可能排列情况数 定义dp[i][j][2],指使用i个步兵,j个骑兵的排列.0代表排头为步兵,1代表排头为骑兵 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define INF 0x3f3f3f3f #define MOD 100000000 usi

Caesar&#39;s Legions(dp)

Caesar's Legions Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description Gaius Julius Caesar, a famous general, loved to line up his soldiers. Overall the army had n1 footmen and n2 horsemen. Caesar tho

D. Caesar&#39;s Legions 背包Dp 递推DP

http://codeforces.com/problemset/problem/118/D 设dp[i][j][k1][k2] 表示,放了i个1,放了j个2,而且1的连续个数是k1,2的连续个数是k2 如果这样写,用dfs写是很简单的.但是超时,我记忆化不到 如果用递推写,对于每一个状态,更新到下一个状态. 如果放的是1,那么新的状态是dp[i + 1][j][k1 + 1][0]也就是,用多了一个1,而且连续的个数也增加了.同时,2的连续个数就打破了,变成了0 这种枚举旧状态,更新下一个状态

HDU 4901 The Romantic Hero(二维dp)

题目大意:给你n个数字,然后分成两份,前边的一份里面的元素进行异或,后面的一份里面的元素进行与.分的时候按照给的先后数序取数,后面的里面的所有的元素的下标一定比前面的大.问你有多上种放元素的方法可以使得前面异或的值和后面与的值相等. dp[x][y] 表示走到第x步,得到y这个数字一共有多少种方法. 但是需要注意这里得分一下,不能直接用dp数组存种数,你需要分一下从上一层过来的次数,和这一层自己可以到达的次数.然后取和的时候前后两个集合的种数进行乘法,注意边乘边取余. 顺便给一组数据: 4 3

二维dp(O(N^3)实现) zoj3230

1 #include<iostream> 2 #include<string.h> 3 #include<stdio.h> 4 #define maxn 125 5 using namespace std; 6 7 int cost[maxn][maxn],w[maxn][maxn]; 8 int dp[maxn][maxn]; 9 int N,M; 10 int main(){ 11 while(cin>>N>>M){ 12 if (N==0

hoj_10014_二维DP

The Triangle Time Limit: 1000ms, Special Time Limit:2000ms, Memory Limit:32768KB Total submit users: 952, Accepted users: 860 Problem 10014 : No special judgement Problem description 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure 1 shows a number tr

XTU1168:Alice and Bob(二维DP)

摘要:Dota(Defence of the Ancients,远古的守护), 是指基于魔兽争霸3:冰封王座(暴雪娱乐公司出品)的多人即时对战自定义地图,可支持10个人同时连线游戏.Dota以对立的两个小队展开对战,通常是5v5,游戏目的是守护自己的远古遗迹(近卫方的生命之树.天灾方的冰封王座),同时摧毁对方的远古遗迹.DotA是目前唯一被暴雪娱乐公司官方认可的魔兽争霸RPG.Dota在大学生中的风靡程度令人咂舌,而随着玩家对游戏的理解深入,本身存在于游戏中的许多数学模型被挖掘出来进行研究.游戏

HDU 5074 Hatsune Miku(简单二维dp)

题目大意:给你一些音符之间的联系,给你一个串,让你求出这个串的最大值.-1的时候可以任意替代,其他情况必须为序列上的数. 解题思路:简单二维dp,分情况处理就可以了啊. Hatsune Miku Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 637    Accepted Submission(s): 458 Problem De