首页 > 代码库 > zoj 3791 An Easy Game dp

zoj 3791 An Easy Game dp

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

题目大意:给两个01字符串s1,s2,经过K次操作每次对M个取反,求有多少种方案使得s1==s2。

dp[i][j]表示i次操作之后有j个位置不同的方法数,答案就是dp[t][0]。对于dp[i - 1][j],经过一次操作之后假设把t个位置从不同变为相同,剩下m - t个位置从相同变为不同。
那么
dp[i][j + m - t - t] += dp[i - 1][j] * C(j, t) * C(n - j, m - t)
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 
 6 typedef __int64 LL;
 7 const int maxn=110;
 8 const int Mod=1000000009;
 9 int n,k,m,C[maxn][maxn];
10 LL d[maxn][maxn];
11 char s1[maxn],s2[maxn];
12 
13 int max(int a,int b){return a>b?a:b;}
14 
15 void init_C()
16 {
17     memset(C,0,sizeof(C));
18     int i,j;
19     for(i=0;i<=100;i++)
20     {
21         C[i][0]=1;
22         for(j=1;j<=i;j++)
23         {
24             C[i][j]=(C[i-1][j-1]+C[i-1][j])%Mod;
25         }
26     }
27 }
28 
29 void solve()
30 {
31     memset(d,0,sizeof(d));
32     int i,j,t,dif=0;
33     for(i=0;i<n;i++) dif+=(s1[i]!=s2[i]?1:0);
34     d[0][dif]=1;
35     for(i=1;i<=k;i++)
36     {
37         for(j=0;j<=n;j++)
38         {
39             for(t=max(0,m-(n-j));t<=j && t<=m;t++)
40             {
41                 d[i][j+m-t-t]=(d[i][j+m-t-t]+d[i-1][j]*C[j][t]%Mod*C[n-j][m-t]%Mod)%Mod;
42             }
43         }
44     }
45     printf("%I64d\n",d[k][0]);
46 }
47 int main()
48 {    
49     init_C();
50     while(~scanf("%d %d %d",&n,&k,&m))
51     {
52         getchar();
53         gets(s1);
54         gets(s2);
55         solve();
56     }
57     return 0;
58 }