Little Sub loves playing the game Flip Me Please. In the game, lights, numbered from 1 to , are connected separately to switches. The lights may be either on or off initially, and pressing the -th switch will change the -th light to its opposite status (that is to say, if the -th light is on, it will be off after the -th switch is pressed, and vice versa).
The game is composed of exactly rounds, and in each round, the player must press exactly 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 and such that , and the -th switch is pressed during the -th round of the first solution while it is not pressed during the -th round of the second solution, or vice versa.
Input
There are multiple test cases. The first line of the input contains an integer (about 1000), indicating the number of test cases. For each test case:
The first line contains three integers , , (, ).
The second line contains a string () consisting of only '0' and '1', indicating the initial status of the lights. If the -th character is '1', the -th light is initially on; If the -th character is '0', the -th light is initially off.
The third line contains a string () consisting of only '0' and '1', indicating the target status of the lights. If the -th character is '1', the -th light must be on at the end of the game; If the -th character is '0', the -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 .
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
Hint
For the first sample test case, Little Sub can press the 1st switch in the 1st round and the 3rd switch in the 2nd round; Or he can press the 3rd switch in the 1st round and the 1st switch in the 2nd round. So the answer is 2.
For the second sample test case, Little Sub can only press the 1st and the 3rd switch in the 1st and only round. So the answer is 1.
這是一個計數DP(應該是,我第一次見到這個詞)
我們場上就沒看出是個DP,我還在分奇偶,下來一聽DP明白了...
轉移方程:dp[i+1][j-l+k-l] += dp[i][j]%MOD*C(l,j)%MOD*C(k-l,n-j)%MOD; C是組合數
二維的dp,一維是第幾輪,第二維是不同棋子的數目,然後會產生一個分歧,如果改變的數量>k,組合數會不正確,所以可以組合數判斷,也可以加個if
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e3+3;
typedef long long LL;
const LL MOD = 998244353;
LL fac[maxn];
LL inv[maxn];
LL C(int m,int n)
{
if(m>n)
return 0;
return fac[n]*inv[m]%MOD*inv[n-m]%MOD;
}
LL quick_MOD(LL a,LL m)
{
LL tmp=a%MOD;
LL ans=1;
while(m)
{
if(m&1)
ans=ans*tmp%MOD;
tmp=tmp*tmp%MOD;
m>>=1;
}
return ans;
}
void init()
{
fac[0]=1;
for(int i=1; i<maxn; i++)
fac[i]=(fac[i-1]*i)%MOD;
inv[maxn-1]=quick_MOD(fac[maxn-1],MOD-2);
for(int i=maxn-2; i>=0; i--)
inv[i]=(inv[i+1]*(i+1))%MOD;
}
LL dp[103][103];
//LL C[103][103];
//void init()
//{
// for(int i=0;i<=100;i++)
// {
// for(int j=0;j<=100;j++)
// {
// if(i == 0 || j == 0)
// C[i][j] = 1;
// else if(i == 1)
// C[i][j] = j;
// else
// {
// C[i][j] = C[i-1][j]*(j-i+1)/i;
// C[i][j] %= MOD;
//// cout<<i<<' '<<j<<C[i][j]<<'\n';
// }
// }
// }
//}
int main()
{
// freopen("in.txt","r",stdin);
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
init();
int t;
cin>>t;
while(t--)
{
int n,m,k;
cin>>n>>m>>k;
string str1,str2;
cin>>str1>>str2;
int diff = 0;
int len = str1.size();
for(int i=0; i<len; i++)
{
if(str1[i] != str2[i])
++diff;
}
memset(dp,0,sizeof(dp));
dp[0][diff] = 1;
for(int i=0; i<m; i++)//輪次
{
for(int j=0; j<=n; j++) //有幾個不同的元素
{
for(int l=0; l<=j; l++) //不同的改變了幾個,剩餘j-l個,+xiao-l
{
if(n-j >= k-l && k-l>=0)
{
dp[i+1][j-l+k-l] += dp[i][j]%MOD*C(l,j)%MOD*C(k-l,n-j)%MOD;
dp[i+1][j-l+k-l] %= MOD;
}
}
}
}
cout<<dp[m][0]<<'\n';
}
return 0;
}
大概跑300-500ms