首页 > 代码库 > BZOJ 1037 生日聚会(神DP)

BZOJ 1037 生日聚会(神DP)

这题的DP很难想,定义dp[i][j][a][b]表示用了i个男生,j个女生,任一连续的后缀区间内,男生比女生最多多a人,女生比男生最多多b人。

转移就是显然了。

 

技术分享
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi acos(-1.0)
# define eps 1e-9
# define MOD 12345678
# define INF 1000000000
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<1,l,mid
# define rch p<<1|1,mid+1,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
int Scan() {
    int res=0, flag=0;
    char ch;
    if((ch=getchar())==-) flag=1;
    else if(ch>=0&&ch<=9) res=ch-0;
    while((ch=getchar())>=0&&ch<=9)  res=res*10+(ch-0);
    return flag?-res:res;
}
void Out(int a) {
    if(a<0) {putchar(-); a=-a;}
    if(a>=10) Out(a/10);
    putchar(a%10+0);
}
const int N=10005;
//Code begin...

int dp[155][155][25][25];

int main ()
{
    int n, m, k;
    scanf("%d%d%d",&n,&m,&k);
    dp[0][0][0][0]=1;
    FOR(i,0,n) FOR(j,0,m) FOR(l1,0,k) FOR(l2,0,k) {
        dp[i+1][j][l1+1][max(l2-1,0)]=(dp[i+1][j][l1+1][max(l2-1,0)]+dp[i][j][l1][l2])%MOD;
        dp[i][j+1][max(l1-1,0)][l2+1]=(dp[i][j+1][max(l1-1,0)][l2+1]+dp[i][j][l1][l2])%MOD;
    }
    int ans=0;
    FOR(i,0,k) FOR(j,0,k) ans=(ans+dp[n][m][i][j])%MOD;
    printf("%d\n",ans);
    return 0;
}
View Code

 

BZOJ 1037 生日聚会(神DP)