Flipping Game
Problem Description
Little Sub loves playing the game Flip Me Please. In the game, nnn lights, numbered from 1 to nnn, are connected separately to nnn switches. The lights may be either on or off initially, and pressing the iii-th switch will change the iii-th light to its opposite status (that is to say, if the iii-th light is on, it will be off after the iii-th switch is pressed, and vice versa).
The game is composed of exactly kkk rounds, and in each round, the player must press exactly mmm different switches. The goal of the game is to change the lights into their target status when the game ends.
Little Sub has just come across a very hard challenge and he cannot solve it. As his friend, it’s your responsibility to find out how many solutions there are to solve the challenge and tell him the answer modulo 998244353.
We consider two solutions to be different if there exist two integers iii and jjj such that 1≤i≤k1 \le i \le k1≤i≤k, 1≤j≤n1 \le j \le n1≤j≤n and the jjj-th switch is pressed during the iii-th round of the first solution while it is not pressed during the iii-th round of the second solution, or vice versa.
Input
There are multiple test cases. The first line of the input contains an integer TTT (about 1000), indicating the number of test cases. For each test case:
The first line contains three integers nnn, kkk, mmm (1≤n,k≤1001 \leq n,k \leq 1001≤n,k≤100, 1≤m≤n1 \leq m \leq n1≤m≤n).
The second line contains a string sss (∣s∣=n|s| = n∣s∣=n) consisting of only ‘0’ and ‘1’, indicating the initial status of the lights. If the iii-th character is ‘1’, the iii-th light is initially on; If the iii-th character is ‘0’, the iii-th light is initially off.
The third line contains a string ttt (∣t∣=n|t| = n∣t∣=n) consisting of only ‘0’ and ‘1’, indicating the target status of the lights. If the iii-th character is ‘1’, the iii-th light must be on at the end of the game; If the iii-th character is ‘0’, the iii-th light must be off at the end of the game.
It is guaranteed that there won’t be more than 100 test cases that n>20n > 20n>20.
Output
For each test case output one line containing one integer, indicating the answer.
Sample Input
3
3 2 1
001
100
3 1 2
001
100
3 3 2
001
100
Sample Output
2
1
7
题意
有n个灯,每个灯对应一个开关,当按下开关后,灯若为开启(关闭)状态,则变为关闭(开启)状态。共有k次操作,每一次必须选择m个开关按下。给出n个灯的最初状态和最后状态,求方案数。
题解:
因为目标是将初始态变为最终态,所以只关心初始态与最终态有多少个位置不同即可。
设dp[i][j]dp[i][j]dp[i][j]代表进行iii次操作后,有jjj个灯的状态与最终态不符。每次操作,枚举s,代表选择s个与最终态不同的灯,m-s个与最终态相同的灯操作,操作后会有t=j−s+m−st=j-s+m-st=j−s+m−s个灯状态与最终态不同,
递推式如下:
dp[i+1][j−s+m−s]=dp[i+1][j−s+m−s]+C(j,s)∗C(n−j,m−s)∗dp[i][j]dp[i+1][j-s+m-s] = dp[i+1][j-s+m-s]+C(j,s)*C(n-j,m-s)*dp[i][j]dp[i+1][j−s+m−s]=dp[i+1][j−s+m−s]+C(j,s)∗C(n−j,m−s)∗dp[i][j]
最后dp[k][0]dp[k][0]dp[k][0]即为方案数。
#include<stdio.h>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<ctype.h>
#include<cstring>
#include<map>
#include<vector>
#include<iterator>
#define dbg(x) cout<<#x<<" = "<<x<<endl;
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3f
#define eps 1e-6
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 120;
const int mod = 998244353;
LL dp[maxn][maxn], C[maxn][maxn];
char s[maxn], t[maxn];
int main()
{
int ca, n, m, i, j, k, tim;
C[1][0] = C[1][1] = C[0][0] = 1;
for(i=2;i<=103;i++)
{
C[i][0] = C[i][i] = 1;
for(j=1;j<i;j++)
C[i][j] = (C[i-1][j]+C[i-1][j-1])%mod;
}
scanf("%d", &ca);
while(ca--)
{
scanf("%d %d %d", &n, &tim, &m);
for(i=0;i<=tim;i++)
memset(dp[i], 0, sizeof(dp[i]));
scanf(" %s %s", s, t);
int num = 0;
for(i=0;i<n;i++)
if(s[i] != t[i])num++;
dp[0][num] = 1;
for(i=0;i<tim;i++)
for(j=0;j<=n;j++)
if(dp[i][j])
{
for(k=0;k<=m;k++)
if(j>=k && n-j>=m-k)
{
int to = j-k + m-k;
dp[i+1][to] = (dp[i+1][to] + C[j][k]*C[n-j][m-k]%mod*dp[i][j]%mod)%mod;
}
}
printf("%lld\n", dp[tim][0]);
}
return 0;
}