Flipping Game ZOJ - 4114 19SD省赛B题(计数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(应该是,我第一次见到这个词)

我们场上就没看出是个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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值