ZOJ 3791 An Easy Game
2014-06-07 21:09
465 查看
这种数据范围一看就想到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.
≤ 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.
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;
}
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;
}
相关文章推荐
- ZOJ 3791 An Easy Game[dp]
- ZOJ 3791 An Easy Game
- ZOJ_3791 An Easy Game[ 二维dp ]
- ZOJ 3791 An Easy Game
- ZOJ 3791 An Easy Game
- ZOJ 3791 An Easy Game DP
- zoj 3791 An Easy Game
- zoj 3791 An Easy Game
- ZOJ 3791 An Easy Game(dp+组合数)
- ZOJ 3791 An Easy Game(DP)
- zoj 3791 An Easy Game dp
- ZOJ_3791_An Easy Game(DP)
- ZOJ 3791 An Easy Game (DP)
- ZOJ 3791 An Easy Game [组合计数]
- zoj 3791 An Easy Game(dp)
- ZOJ 3791 An Easy Game[dp]
- poj-3791-An Easy Game-记忆化搜索
- poj-3791-An Easy Game-记忆化搜索
- ZOJ3791_An Easy Game
- 校赛 An Easy Number Game 回溯