Codeforces-118D. Caesar'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
using namespace std;
typedef long long LL;

int N1, N2, K1, K2;
const int maxn = 110;
int dp[maxn][maxn][2];

int main() {
    scanf("%d%d%d%d", &N1, &N2, &K1, &K2);
    for (int i = 0; i <= K1; i++) dp[i][0][0] = 1;
    for (int i = 0; i <= K2; i++) dp[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++) {
                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;
            }
        }
    }
    int ans = (dp[N1][N2][0] + dp[N1][N2][1]) % MOD;
    printf("%d\n", ans);

    return 0;
}

Codeforces-118D. Caesar's Legions(lazy dynamics)

原文地址:https://www.cnblogs.com/xFANx/p/8436652.html

时间: 2025-02-01 17:49:11

Codeforces-118D. Caesar's Legions(lazy dynamics)的相关文章

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 这种枚举旧状态,更新下一个状态

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

codeforces118D - Caesar&#39;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 #i

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

Codeforces - 102222C - Caesar Cipher

https://codeforc.es/gym/102222/my 好像在哪里见过这个东西?字符的左右移还是小心,注意在mod26范围内. #include<bits/stdc++.h> using namespace std; typedef long long ll; inline int read() { int x=0; int f=0; char c; do { c=getchar(); if(c=='-') f=1; } while(c<'0'||c>'9'); do

Codeforces 242E. XOR on Segment (二维线段树 lazy操作 xor)

题目链接: http://codeforces.com/problemset/problem/242/E 题意: 给出一个序列,有两种操作,一种是计算l到r的和,另一种是让l到r的数全部和x做异或运算. 思路: from: http://blog.csdn.net/u013912596/article/details/39006317 很显然直接暴力是不可能的,又是两种操作,又想到了线段树..但是这并不简单,异或操作该怎么处理? 异或是一种位运算,如果x的第j位是1,那么说明l到r的每个数的第j

Codeforces Round #335 (Div. 2) D. Lazy Student 贪心

D. Lazy Student Student Vladislav came to his programming exam completely unprepared as usual. He got a question about some strange algorithm on a graph — something that will definitely never be useful in real life. He asked a girl sitting next to hi

CodeForces 605B Lazy Student

构造.对边的权值排序,权值一样的话,在MST中的边排到前面,否则权值小的排在前面. 然后边一条一条扫过去,如果是1 ,那么连一个点到集合中,如果是0,集合内的边相连. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int maxn=100000+10; struct Edge { int u,v; int w;

codeforces #335div2 D. Lazy Student 构造

#include<bits/stdc++.h> #define REP(i,a,b) for(int i=a;i<=b;i++) #define MS0(a) memset(a,0,sizeof(a)) using namespace std; typedef long long ll; const int maxn=1000100; const int INF=1<<29; int n,m; struct Edge { int w,is; int id; friend bo