Caesar's Legions(记忆化搜索)

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 beautiful arrangements 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.

Input
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.

Output
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.

Sample test(s)
input
2 1 1 10
output
1
input
2 3 1 2
output
5
input
2 4 1 1
output
0
Note
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

有n1个步兵,n2个骑兵,步兵最多连续站k1个,骑兵最多连续站k2个,问多少种站法

写了这么久终于遇到自己还能搞的dp了……然而第一次一下就想到,第二次写就跪了……

就是一个dfs,n1,n2,前一个是骑兵还是步兵这三个数据传进去就ok了。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1005;
int mod = 1e8;
int dp[105][105][2];
int k1,k2;
int dfs(int n1,int n2,int flag)//flag中01表示前一个是骑兵还是步兵。
{
    int& temp = dp[n1][n2][flag];
    if(temp != -1)return temp;
    if(n1 == 0&&n2 == 0)return temp = 1;
    temp = 0;
    if(flag)
    {
        for(int i = 1;i <= min(n2,k2);i++)
        {
            temp = (temp + dfs(n1,n2 - i,0))%mod;
        }
    }
    else
    {
        for(int i = 1;i <= min(n1,k1);i++)
        {
            temp = (temp + dfs(n1 - i,n2,1))%mod;
        }
    }
    return temp;
}
int main()
{
    #ifdef LOCAL
    freopen("C:\\Users\\巍巍\\Desktop\\in.txt","r",stdin);
    //freopen("C:\\Users\\巍巍\\Desktop\\out.txt","w",stdout);
    #endif // LOCAL
    int n1,n2;
    scanf("%d%d%d%d",&n1,&n2,&k1,&k2);
    memset(dp,-1,sizeof(dp));
    int ans = (dfs(n1,n2,0) + dfs(n1,n2,1))%mod;
    printf("%d\n",ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值