ZOJ 3791 An Easy Game

本文讨论了一种使用动态规划(DP)解决特定游戏变换问题的方法。通过改变字符串中指定数量的位置,目标是将一个字符串转换为另一个字符串。文章详细解释了如何通过动态规划计算在给定步骤下实现目标字符串的不同方式的数量,同时考虑到字符串长度、步骤限制和位置更改的约束。


这种数据范围一看就想到DP....

dp[i][j]:在第i轮有j个不同位置的可能的方法数,配合组合数搞一搞就可以了。。。注意超INT


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 ≤ m ≤ n). 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



#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
const long long int  MOD = 1000000009LL;
long long  C[110][110],dp[110][110];
int n,m,K;
char s1[110],s2[110];

int main()
{
    for(int i=0;i<110;i++) C[i][i]=1LL,C[i][0]=1LL;
    for(int i=2;i<110;i++) for(int j=1;j<i;j++) C[i][j]=(C[i-1][j]+C[i-1][j-1])%MOD;
    while(scanf("%d%d%d",&n,&m,&K)!=EOF)
    {
        memset(dp,0,sizeof(dp));
        scanf("%s%s",s1,s2);
        int nt=0;
        for(int i=0;i<n;i++)
            if(s1[i]!=s2[i]) nt++;
        dp[0][nt]=1;
        for(int i=1;i<=m;i++)
        {
            for(int j=0;j<=n;j++)
            {
                for(int k=max(0,j-K);k<=min(n,j+K);k++)
                {
                    ///   dp[i][j]+=dp[i-1][k]*C[][]*C[][];
                    int deta=j-k;///不同的位置个数变化量
                    if(deta>=0)
                    {
                        if(deta==K||(K-deta)%2==0)
                        {
                            dp[i][j]=(dp[i][j]+((dp[i-1][k]*C[k][(K-deta)/2])%MOD*C[n-k][deta+(K-deta)/2])%MOD)%MOD;
                        }
                    }
                    else if(deta<0)
                    {
                        deta*=-1;
                        if(deta==K||(K-deta)%2==0)
                        {
                            dp[i][j]=(dp[i][j]+((dp[i-1][k]*C[k][deta+(K-deta)/2])%MOD*C[n-k][(K-deta)/2])%MOD)%MOD;
                        }
                    }
                }
            }
        }
        printf("%lld\n",dp[m][0]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值