ZOJ 3791 An Easy Game[dp]

25 篇文章 0 订阅
6 篇文章 0 订阅

One day, Edward and Flandre play a game. Flandre will show two 01-strings s1 and s2, the lengths of two strings are n. Then, Edward must move exact k steps. In each step, Edward should change exact m positions of s1. That means exact m positions of s1, '0' will be changed to '1' and '1' will be changed to '0'.

The problem comes, how many different ways can Edward change s1 to s2 after k steps? Please calculate the number of the ways mod 1000000009.

Input

Input will consist of multiple test cases and each case will consist of three lines. The first line of each case consist of three integers n (1 ≤ n ≤ 100), k (0 ≤ k ≤ 100), m (0 ≤ mn). The second line of each case is a 01-string s1. The third line of each case is a 01-string s2.

Output

For each test case, you should output a line consist of the result.

Sample Input
3 2 1
100
001
Sample Output
2
Hint
100->101->001
100->000->001

题意:给两个01串A和B,长度都是n。共进行k次操作,每次需翻转m个位,问有多少种方法将A转换成B。


两个串哪位是1哪位是0是无关紧要的,因为不要求翻转连续的几位。要关注的是它们有多少位不相同。

d[i][j]表示第i步操作后有j位不相同的方法数。

先把100以内的组合数推出来然后慢慢搞搞就行。

d[i][j] = d[i-1][s]*C[s][x]*C[n-s][y]

解释一下:s是从上一步可以推导过来的一些状态,即上一步完成后有s位不相同,

显然 max{0,j-m} <= s <= min{n,j+m} 。而且s还与j和m的奇偶性有关。j和m奇偶性相同则s为偶,否则为奇。

每一步操作,我们都可以在上一种状态下先减少x位不同,再增加y位不同,使得此次操作完后不同的数目变成j。每次翻转m位,即x+y=m。

最后的答案是 d[k][0]。



#include<cassert>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<set>
#include<queue>
#include<map>
using namespace std;
#define rep(i,f,t) for(int i = (f), _end = (t); i <= _end; ++i)
#define clr(c,x) memset(c,x,sizeof(c));
#define debug(x) cout<<"debug  "<<x<<endl;
const int INF = 0x3f3f3f3f;
typedef long long int64;

inline int RD(){
    int res;
    scanf("%d",&res);
    return res;
}

#define Rush for(int casn = RD(), cas = 1; cas <= casn; ++cas)

//*******************************************************************************
const int maxn = 110;
int64 d[maxn][maxn];
char s1[maxn],s2[maxn];
int n,m,k;
int64 C[maxn][maxn];
const int mod = 1000000009;

void calC(){
    C[0][0] = 1;
    rep(i,1,100)rep(j,0,i){
        if(j == 0 || i == j){ C[i][j] = 1; continue; }
        if(i - j >= j)
            C[i][j] = (C[i-1][j] + C[i-1][j-1]) % mod;
        else
            C[i][j] = C[i][i-j];
    }
}
int main(){
    //freopen("3791.in","r",stdin);
    //freopen("3791.out","w",stdout);
    calC();
    while(scanf("%d%d%d",&n,&k,&m) == 3){
        scanf("%s%s",s1,s2);
        int cnt = 0;
        rep(i,0,n-1) cnt += s1[i] != s2[i]; //不同的个数
        clr(d,0);
        d[0][cnt] = 1;
        rep(i,1,k){
            d[i][0] = d[i-1][m];
            rep(j,1,n){
                int oe = (j&1) != (m&1);    //s的奇偶性
                rep(s,max(j-m,0),min(j+m,n)){ //从d[i-1][s]推导过来
                    if((s&1) != oe)continue;
                    if(d[i-1][s] == 0)continue;
                    int dt = s - j; //相差的数目(减少的数)
                    int x = (m + dt) >> 1;
                    int y = m - x;
                    if(x <= s && y <= n-s){
                        d[i][j] = (d[i][j] + d[i-1][s] * C[s][x] % mod * C[n-s][y] % mod) % mod;
                    }
                }
            }
        }
        printf("%lld\n",d[k][0]);
    };
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值