B - Flipping Game DP

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,就算想出来了估计也做不出来。

题目大意是说有n个开关,有起始状态和终状态,问如果每次必须选m个开关进行改变状态,一共进行k次,那么有多少种方式可以从起始状态到终状态。

有这么一个规律,初始状态和末尾状态,开关状态变化了的开关,其一定操作了奇数次,这个不难推出来,因为如果是偶数次相当于没有操作。用一个二维的dp数组来记录动态规划的过程,dp[i][j]表示第i轮操作中有j个位置操作了奇数次。

接下来再推一下状态转移方程,假设这一次操作中,k个开关从操作次数为奇数变成了偶数,那么有m-k个开关操作次数从偶数变成了奇数,每次变化中,利用组合数来求得可能的情况。所以有:dp[i+1][j-k+m-k]+=dp[i][j]*c[j][k]*c[n-j][m-k],其中乘以c[j][k]是说对所有可能的开关中选择k个变成偶数操作次数,再乘以c[n-j][m-k]是说在这次操作中选择剩下的那部分来改成操作次数为奇数。最后答案即为dp[k][0]

AC代码

#include
#include
#include
using namespace std;
const int MOD = 998244353;
long long int dp[105][105];
long long int c[105][105];
char s1[105],s2[105];
int main()
{
    for(int i=0;i<=100;i++)
		c[i][0]=1;
   	for(int i=1;i<=100;i++)
	   c[i][i]=1;
    for(int i=2;i<=100;i++)
    	for(int j=1;j<=i-1;j++)
            c[i][j]=(c[i-1][j-1]+c[i-1][j])%MOD;
            
    int t,n,k,m;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d%d%s%s",&n,&k,&m,s1,s2);
        int num=0;
        for(int i=0;i=k&&n-j>=m-k){
                        dp[i+1][j-k+m-k]+=dp[i][j]*c[j][k]%MOD*c[n-j][m-k]%MOD;
                        dp[i+1][j-k+m-k]%=MOD;
                    }
        printf("%lld\n",dp[k][0]);
    }
    return 0;
}

你可能感兴趣的:(2019,ACM,山东省赛)