Zoj?#an easy game

Source: Internet
Author: User
Tags first string

Two equal-length strings are given. Each time M numbers are required to be changed, K numbers must be changed each time, and the number of solutions from the first string to the second string must be calculated.

DP. F [I] [J] Number of solutions with J locations changed after step I is changed. Then, we can directly enumerate the positions that are currently changed, which are overlapped.

Then output the statistical answer.

 

 

#include <iostream>#include <cstring>#include <cstdio>#define M 1000000009#define maxn 105typedef long long ll;using namespace std;ll C[maxn][maxn];ll f[maxn][maxn];int n,k,m,change;ll ans;ll power(ll A,ll B){    ll tot=1;    while (B){        if (B&1) tot=tot*A%M;        A=A*A%M,B>>=1;    }    return tot;}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]+C[i-1][j-1])%M;    }}int main(){    _init();    char s1[maxn],s2[maxn];    while (scanf("%d%d%d",&n,&k,&m)!=EOF){        change=0;        scanf("%s%s",s1,s2);        for (int i=0; i<n; i++)            if (s1[i]!=s2[i]) change++;        memset(f,0,sizeof f);        f[0][0]=1;        for (int i=0; i<k; i++)//after the ith time of changes            for (int j=0; j<=n; j++){//the number of 1 is j                if (f[i][j]==0) continue;                for (int x=max(0,j+m-n); x<=min(j,m); x++){                    f[i+1][j-x+m-x]+=f[i][j]*(C[j][x]*C[n-j][m-x]%M)%M;                    f[i+1][j-x+m-x]%=M;                }            }        ans=f[k][change]*power(C[n][change],M-2)%M;        printf("%d\n",(int)ans);    }    return 0;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.