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;}