ZOJ 3791 An Easy Game DP

来源:互联网 发布:光敏刻章机软件下载 编辑:程序博客网 时间:2024/06/06 00:08

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3791

http://vjudge.net/contest/view.action?cid=52151#problem/D

浙大月赛


1.题意:

将一个长度为n的0-1串,变为另一个长度为n的0-1串,执行k次操作,每次操作将m个位置从0变为1或从1变为0,问你有多少种操作方法可以使源串变为目标串。

2.题解

(1)用dp[i][j]表示源串第i次操作后,与目标串不同的位置有几个。

(2)对于dp[i][j],有j个位置与目标串不同,所以可以将j个位置中的t个位置变为与目标串相同,把其余n-j个位置中的m-t个位置变为与目标串不同,转移方程就是dp[i][j-t+m-t]+=dp[i][j]*C(t,j)*C(m-t,n-j)

(3)如果j+m>n,那么t就无法从0开始枚举了,所以此时t从(m-(n-j))开始(m次操作中有n-j次操作落在n-j个相同位置的里面,还有m-(n-j)次操作落在j个不同位置的里面)。

(4)初始条件就是用cnt统计源串和目标串有多少个位置不一样,dp[0][cnt]=1,通过dp把路径都找出来,那么答案就是dp[k][0]。


code:

#include <iostream>#include <cstdio>using namespace std;typedef long long LL;const LL MOD=1000000009;LL dp[111][111],c[111][111];char s[111],d[111];void _init(){    c[0][0]=1;    for(int i=1;i<=100;i++){        c[i][0]=1;        for(int j=1;j<=i;j++){            c[i][j]=(c[i-1][j-1]+c[i-1][j])%MOD;        }    }}int main(){    int n,k,m, i,j,t,x;    _init();    while(scanf("%d%d%d",&n,&k,&m)==3){        scanf("%s",s);        scanf("%s",d);        x=0;        for(i=0;i<n;i++)            if(s[i]!=d[i])x++;        for(i=0;i<=k;i++)            for(j=0;j<=n;j++)            dp[i][j]=0;        dp[0][x]=1;        for(i=0;i<k;i++)//ith operation            for(j=0;j<=n;j++){//j different                if(dp[i][j]==0)continue;                for(t=max(0,m-(n-j));t<=j&&t<=m;t++)//j+m should no biger than n or at least (m-(n-j)) should be change                    dp[i+1][j-t+m-t]=(dp[i+1][j-t+m-t]+dp[i][j]*c[j][t]%MOD*c[n-j][m-t]%MOD)%MOD;            }        LL ans=dp[k][0];        printf("%lld\n",ans);    }    return 0;}


0 0
原创粉丝点击