ZOJ 4114 Flipping Game 动态规划

题目链接:ZOJ 4114 Flipping Game

题目

Description

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.

题目思路

其实就是一个很简单的 d p dp dp问题,但是因为组合数的问题一直TLE,QAQ。
题目大意就是有 n n n台灯,每台灯都有一个开关,可以改变其明暗状态,给你 k k k轮,每轮改变 m m m台不同的灯的明暗(必须用完且不能重复修改同一台灯),问你有多少种方法将其由初始状态变为目标状态。
注意到,目标状态和初始状态有四种对应关系:01,10,11,00。但是仔细观察就会发现,11和00是没有差别的,因为都是要求在总过程中变化次数为偶数(原因很容易理解,这里不再赘述)。同理,10和01也是没有区别的。所以,其实我们只需要记录开始时有多少个点的起始状态与目标状态不同,然后求有多少种方案把这个数值变为0即可。
这里用 d p [ i ] [ j ] dp[i][j] dp[i][j]记录在经过 i i i轮后,剩余不同步点的数目为 j j j,由于在改变的过程中不能在同一轮改变同一个点,所以要使剩余灯数从 a a a达到 b b b,必须要保证有 x x x盏灯从相同到不同, m − x m-x mx盏灯从相同到不同, ∣ m − 2 x ∣ = ∣ a − b ∣ |m-2x|=|a-b| m2x=ab,这里正负视具体情况而定。,设那么就有状态转移方程 d p [ i ] [ j ] = ∑ k = 0 n d p [ i − 1 ] [ k ] ∗ C k x ∗ C n − k m − x ( x = ( k + m − j ) / 2 , 且 ( k + m − j ) % 2 = = 0 ) dp[i][j]=\sum_{k=0}^{n}{dp[i-1][k]*C_k^x*C_{n-k}^{m-x}}(x=(k + m -j) / 2,且(k+m-j)\%2==0) dp[i][j]=k=0ndp[i1][k]CkxCnkmx(x=(k+mj)/2,(k+mj)%2==0)这个方程看着有点长,2333,但其实不难理解,我从原来只有 k k k个不同点到现在有 j j j个不同点,把所有k都试一遍,看看能不能到达 j j j,如果能,则有以上式子 d p [ i − 1 ] [ k ] ∗ C k x ∗ C n − k m − x dp[i-1][k]*C_k^x*C_{n-k}^{m-x} dp[i1][k]CkxCnkmx种可能,总可能就是这些情况累加。注意要先判断 k k k能否到达 j j j,如果 ∣ k − j ∣ > m |k-j|>m kj>m或者说 ( k + m − j ) % 2 ! = 0 (k+m-j)\%2!=0 (k+mj)%2!=0显然都是不可以到达的。最后,也就是我TLE到哭的地方,组合数一开始打表处理,用 C n m = C n − 1 m − 1 + C n − 1 m C_n^m=C_{n-1}^{m-1}+C_{n-1}^{m} Cnm=Cn1m1+Cn1m会大大减少运行时间。
上代码:

#include <cstdio>
#include <cstring>
using namespace std;

#define ll long long
#define maxn 105
const ll mod = 998244353;
ll dp[maxn][maxn];
ll C[maxn][maxn];
char s[maxn], tmp;
ll abs(ll x) {return x > 0? x: -x;}

void solve()
{
	memset(dp, 0, sizeof(dp));
	int n, k, m, dif = 0;
	scanf("%d%d%d", &n, &k, &m);
	scanf("\n");
	for(int i = 1; i <= n; i++)
		scanf("%c", &s[i]);
	scanf("\n");
	for(int i = 1; i <=n; i++){
		scanf("%c", &tmp);
		if(tmp != s[i]) dif++;
	}
	dp[0][dif] = 1;
	for(int i = 1; i <= k; i++){
		for(int j = 0; j <= n; j++){
			for(int t = 0; t <= n; t++){
				if(t + m < j || t - m > j) continue;
				if((t + m -j) % 2 != 0) continue;
				ll x = (t + m -j) / 2;
				dp[i][j] += dp[i-1][t] * C[t][x] % mod * C[n-t][m-x] % mod;
				dp[i][j] %= mod;				
			}
		}		
	}
	printf("%lld\n", dp[k][0]);
}

int main()
{
	memset(C, 0, sizeof(C));
	C[0][0] = C[1][1] = 1;
	for(int i = 1; i <= 100; i++) C[i][0] = 1;
	for(int i = 1; i <= 100; i++)
		for(int j = 1; j <= i; j++)
			C[i][j] = (C[i-1][j] + C[i-1][j-1]) % mod;
	int kase;
	scanf("%d", &kase);
	for(int i = 0; i < kase; i++)
		solve();
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值