codeforces118——D. Caesar‘s Legions(DP)

原题链接
题意:
在这里插入图片描述
1<=k1,k2<=10,1<=n1,n2<=100
思路:
开了个四维的DP,反正空间也足够(手动狗头.jpg)
dp[i][j][u][v]表示选了i个A,j个B,而且末尾有u个连续的A或v个连续的B的方案数。
因为结尾只能放一种字符,所以u和v中肯定有一个是0。(从这点可以简化成三维的DP)
当u==0时,表示末尾有v个连续的B,考虑下一个放什么,如果下一个放A,那么该状态就能转移到dp[i+1][j][1][0];如果下一个放B,那么该状态就能转移到dp[i][j+1][0][v+1]。
当v ==0时也同理。
初始化就是放第一个,结果的话对于所有的进行求和。
代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll>PLL;
typedef pair<int,int>PII;
typedef pair<double,double>PDD;
#define I_int ll
inline ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
char F[200];
inline void out(I_int x)
{
    if (x == 0) return (void) (putchar('0'));
    I_int tmp = x > 0 ? x : -x;
    if (x < 0) putchar('-');
    int cnt = 0;
    while (tmp > 0)
    {
        F[cnt++] = tmp % 10 + '0';
        tmp /= 10;
    }
    while (cnt > 0) putchar(F[--cnt]);
    //cout<<" ";
}
ll ksm(ll a,ll b,ll p)
{
    ll res=1;
    while(b)
    {
        if(b&1)res=res*a%p;
        a=a*a%p;
        b>>=1;
    }
    return res;
}
const ll inf = 0x3f3f3f3f3f3f3f3f;
const int maxn=1100,mod=1e8;
const double PI = atan(1.0)*4;
const double eps=1e-6;
int dp[105][105][15][15];
int main ()
{
    int n1,n2,k1,k2;
    n1=read(),n2=read(),k1=read(),k2=read();
    dp[1][0][1][0]=dp[0][1][0][1]=1;
    for(int i=0; i<=n1; i++)
    {
        for(int j=0; j<=n2; j++)
        {
            for(int u=0; u<=min(i,k1); u++)
            {
                dp[i+1][j][u+1][0]=(dp[i+1][j][u+1][0]+dp[i][j][u][0])%mod;
                dp[i][j+1][0][1]=(dp[i][j+1][0][1]+dp[i][j][u][0])%mod;

            }
            for(int v=0; v<=min(j,k2); v++)
            {
                dp[i][j+1][0][v+1]=(dp[i][j+1][0][v+1]+dp[i][j][0][v])%mod;
                dp[i+1][j][1][0]=(dp[i+1][j][1][0]+dp[i][j][0][v])%mod;

            }
        }
    }
    int res=0;
    for(int i=0; i<=min(k1,n1); i++) res=(res+dp[n1][n2][i][0])%mod;
    for(int i=0; i<=min(k2,n2); i++) res=(res+dp[n1][n2][0][i])%mod;
    out(res);
    return 0;
}
原文地址:https://www.cnblogs.com/OvOq/p/14853076.html