ZOJ_4114 Flipping Game(动态规划)

Flipping Game

Time Limit: 1000 ms
Memory Limit: 65536 KB
Problem Description

Little Sub loves playing the game Flip Me Please. In the game, n n n lights, numbered from 1 to n n n, are connected separately to n n n switches. The lights may be either on or off initially, and pressing the i i i-th switch will change the i i i-th light to its opposite status (that is to say, if the i i i-th light is on, it will be off after the i i i-th switch is pressed, and vice versa).

The game is composed of exactly k k k rounds, and in each round, the player must press exactly m m m 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 i i i and j j j such that 1 ≤ i ≤ k 1 \le i \le k 1ik, 1 ≤ j ≤ n 1 \le j \le n 1jn and the j j j-th switch is pressed during the i i i-th round of the first solution while it is not pressed during the i i i-th round of the second solution, or vice versa.

Input

There are multiple test cases. The first line of the input contains an integer T T T (about 1000), indicating the number of test cases. For each test case:

The first line contains three integers n n n, k k k, m m m ( 1 ≤ n , k ≤ 100 1 \leq n,k \leq 100 1n,k100, 1 ≤ m ≤ n 1 \leq m \leq n 1mn).

The second line contains a string s s s ( ∣ s ∣ = n |s| = n s=n) consisting of only ‘0’ and ‘1’, indicating the initial status of the lights. If the i i i-th character is ‘1’, the i i i-th light is initially on; If the i i i-th character is ‘0’, the i i i-th light is initially off.

The third line contains a string t t t ( ∣ t ∣ = n |t| = n t=n) consisting of only ‘0’ and ‘1’, indicating the target status of the lights. If the i i i-th character is ‘1’, the i i i-th light must be on at the end of the game; If the i i i-th character is ‘0’, the i i i-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 > 20 n > 20 n>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个灯的最初状态和最后状态,求方案数。

题解:

因为目标是将初始态变为最终态,所以只关心初始态与最终态有多少个位置不同即可。
d p [ i ] [ j ] dp[i][j] dp[i][j]代表进行 i i i次操作后,有 j j j个灯的状态与最终态不符。每次操作,枚举s,代表选择s个与最终态不同的灯,m-s个与最终态相同的灯操作,操作后会有 t = j − s + m − s t=j-s+m-s t=js+ms个灯状态与最终态不同,
递推式如下:
d p [ i + 1 ] [ j − s + m − s ] = d p [ i + 1 ] [ j − s + m − s ] + C ( j , s ) ∗ C ( n − j , m − s ) ∗ d p [ 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][js+ms]=dp[i+1][js+ms]+C(j,s)C(nj,ms)dp[i][j]
最后 d p [ 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值