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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章