bzoj1037

一道MLE的DP,我们设f[i][j][x][y]表示一共i个人j个男生,男生比女生多x个,女生比男生多y个。递推

如果x+1<k,f[i+1,j+1,x+1,max(y-1,0)]+=f[i][j][x][y]

如果y+1<k,f[i+1,j,max(x-1,0),y+1]+=f[i][j][x][y]

最后统计答案,不要忘了取膜

#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
const int lxn=12345678;
int n,m,k,ans;
int f[301][151][21][21]; 
template <class T> void read(T&x)
{
  x=0;char c=getchar();int f=0;
  while(c<'0'||c>'9'){f|=(c=='-');c=getchar();}
  while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+(c^=48),c=getchar();
  x=f?-x:x;
}
int main()
{ 
  read(n);read(m);read(k);
  f[0][0][0][0]=1;
  for(int i=0;i<n+m;i++)
   for(int j=0;j<=n;j++)
    for(int x=0;x<=k;x++)
     for(int y=0;y<=k;y++)
     {
       if(x+1<=k&&j+1<=n)
       {
            f[i+1][j+1][x+1][max(y-1,0)]+=f[i][j][x][y];
            f[i+1][j+1][x+1][max(y-1,0)]%=lxn;
       }
       if(y+1<=k&&i+1-j<=m)
       {
            f[i+1][j][max(x-1,0)][y+1]+=f[i][j][x][y];
            f[i+1][j][max(x-1,0)][y+1]%=lxn;
       }
     }
  int ans=0;
  for(int i=0;i<=n;i++)
   for(int x=0;x<=k;x++)
    for(int y=0;y<=k;y++)
    {
      ans+=f[n+m][i][x][y];
      ans%=lxn;
    }
  printf("%d",ans);
  return 0;
}
View Code

 

posted @ 2017-11-03 14:18  新手-周  阅读(132)  评论(0编辑  收藏  举报