dp的开关路灯
2014-08-22 09:52
162 查看
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.
≤ 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.
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
#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; }
相关文章推荐
- 关路灯、Grazing on the run---很难的dp
- lamp 开关灯 (dp)
- codevs1258 关路灯(☆区间dp)
- [luogu]P1220关路灯-区间DP
- 洛谷P1220关路灯[区间DP 提前计算代价]
- 【vjios1488】【分组背包+dp优化】路灯改建计划
- 关路灯_洛谷1220_dp
- 关路灯(区间dp)
- [区间dp] 洛谷 P1220 关路灯
- 洛谷P1220 关路灯(DP或深搜)
- P1220 关路灯(区间dp)
- 109.关路灯(区间dp)
- 洛谷 P1220 关路灯 (区间DP)
- [luoguP1220] 关路灯(DP)
- 【DP】洛谷 P1220 关路灯
- 网站开关设计及模板更换设计
- nyoj 571 整数划分问题(dp)
- hdu 3555 数位dp
- uva 12723 概率dp
- UVA 10617 区间DP