ZOJ 3791 An Easy Game (DP)

39 篇文章 0 订阅

An Easy Game

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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

Author: CHEN, Zemin
Source: ZOJ Monthly, June 2014


题意:

有两个长度相同的01串AB,目标把A经过K次变化刚好变成B,每次变化必须选择A中m位取反,0->1 1->0 问有多少中方案mod 1000000009


思路:DP

没想到状态中01的位置没影响!直接用不同的位置个数标示状态,然后每次选择m变化个转移到另外的状态!


#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

using namespace std;

//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#endif

#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define FOR(i, s, t) for(int (i)=(s); (i)<(t); (i)++)

const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double PI = (4.0*atan(1.0));

const int maxn = 100 + 20;
const int mod = 1000000009;
int A[maxn];
int B[maxn];
LL dp[maxn][maxn];
LL C[maxn][maxn];

void init() {
    memset(C, 0, sizeof(C));
    C[0][0] = 1;
    for(int i=1; i<maxn; i++) {
        C[i][0] = 1;
        for(int j=1; j<=i; j++) {
            C[i][j] = C[i-1][j-1] + C[i-1][j];
            if(C[i][j] >= mod) C[i][j] -= mod;
        }
    }
}

int main() {
    int n, k, m;

    init();
    while(scanf("%d%d%d", &n, &k, &m) != EOF) {
        for(int i=0; i<n; i++) {
            char c = getchar();
            while(c!='0' && c!='1') c = getchar();
            A[i] = c - '0';
        }
        int dif = 0;
        for(int i=0; i<n; i++) {
            char c = getchar();
            while(c!='0' && c!='1') c = getchar();
            B[i] = c - '0';
            if(B[i] != A[i]) dif++;
        }
        memset(dp, 0, sizeof(dp));
        dp[0][dif] = 1;
        for(int i=0; i<k; i++) {
            for(int j=0; j<=n; j++) {
                for(int p=min(j, m); p>=0; p--) {
                    if(m-p > n-j) continue;
                    int tnt = m-p;
                    int tyt = j-p;
                    int nd = tnt + tyt;
                    LL t = dp[i][j] * C[j][p] % mod;
                    t = t * C[n-j][tnt] % mod;
                    dp[i+1][nd] = (dp[i+1][nd] + t) % mod;
                }
            }
        }
        P64I(dp[k][0]);
    }

    return 0;
}





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值