[ZJOI] 2008 birthday party

 


 

Obviously DP.

The conversion under Title: seeking by n 0, m 1 a composition, and to meet any number of sub-string 01 string 0 and the number of the number of the difference absolute value is not more than 1 k. n, m≤150, k≤20.

Thinking nothing directly to do ,, then we try to take advantage of the subject of time and space constraints , to achieve a clear, easy transfer equation.

 


 

 

Since the Title n, m is small, it may be considered O (nm) several side band algorithm k.

Set f [a] [b] [ l] [h] represents the current sequence put a a a 1, b 0's, all suffixes, the difference male minus female of the maximum reduced boys was L, female difference between the maximum of h. the number of programs.

Then the state transition is clearly -> f [a] [b] [l] [h] may be the f [a + 1] [b] [l + 1] [max (h-1), 0] or f [a] [b + 1] [max (l-1)] [h + 1] metastasis; final answer statistical f [n] [m] [l] [h] (enumeration l, h).

 

Code:

#include<bits/stdc++.h>
using namespace std;

const int maxn = 180;
const int mod = 12345678;

int f[maxn][maxn][26][26];

int main(){
    int n,m,k;
    cin>>n>>m>>k;
    f[0][0][0][0] = 1;
    for(int a = 0;a<=n;++a)
    for(int b = 0;b<=m;++b)
    for(int l = 0;l<=k;++l)
    for(int h = 0;h<=k;++h)
    if(f[a][b][l][h]){
        int cnt = f[a][b][l][h];
        f[a+1][b][l+1][max(h-1,0)] += cnt;f[a+1][b][l+1][max(h-1,0)]%=mod;
        f[a][b+1][max(l-1,0)][h+1] += cnt;f[a][b+1][max(l-1,0)][h+1]%=mod;
    }
    int ans = 0;
    for(int i = 0;i<=k;++i)
        for(int j = 0;j<=k;++j)
        ans = (ans + f[n][m][i][j])%mod;
    cout<<ans<<endl;
}

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/guoyangfan/p/11237290.html