ZOJ_4114 Flipping Game(动态规划)

本文探讨了一款名为“FlipMePlease”的翻转游戏策略求解问题,游戏包含n个可独立控制的灯,目标是在k轮中通过每次选择m个开关来改变灯的状态,从初始状态达到目标状态。文章提出了一种动态规划方法,通过计算不同状态下可能的操作组合数量,以确定解决挑战的方案数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Flipping Game

Time Limit: 1000 ms
Memory Limit: 65536 KB
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 k1ik, 1≤j≤n1 \le j \le n1jn 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 1001n,k100, 1≤m≤n1 \leq m \leq n1mn).

The second line contains a string sss (∣s∣=n|s| = ns=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| = nt=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=js+ms个灯状态与最终态不同,
递推式如下:
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][js+ms]=dp[i+1][js+ms]+C(j,s)C(nj,ms)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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值