B - Flipping Game (组合数学+dp+思维)
Posted zhangbuang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 .
<h4< dd="">Output
For each test case output one line containing one integer, indicating the answer.
<h4< dd="">Sample Input
3 3 2 1 001 100 3 1 2 001 100 3 3 2 001 100
<h4< dd="">Sample Output
2 1 7
<h4< dd="">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题目,但是状态不好找,如果按状压的思想的话,在我选m个同的灯泡的时候会很难去处理,因为这里用到了组合数,
所以就要在初始条件和最终条件之间关系,那就是设有几个不一样的
1 #include<iostream> 2 #include<stdio.h> 3 #include<math.h> 4 #include<algorithm> 5 #include<string> 6 #include<cstring> 7 using namespace std; 8 9 typedef long long ll; 10 11 const int INF=0x3f3f3f3f; 12 const int MAX_N=1e5+5; 13 const int M=998244353; 14 15 int n,k,m; 16 ll dp[105][105]; 17 ll fac[105],inv_fac[105]; 18 19 ll mod_pow(ll x,ll n){ 20 ll res=1; 21 while(n>0){ 22 if(n&1)res=res*x%M; 23 x=x*x%M; 24 n>>=1; 25 } 26 return res; 27 } 28 29 void init(){ 30 fac[0]=1; 31 for(int i=1;i<105;i++){ 32 fac[i]=fac[i-1]*i%M; 33 } 34 inv_fac[104]=mod_pow(fac[104],M-2); 35 for(int i=103;i>=0;i--){ 36 inv_fac[i]=inv_fac[i+1]*(i+1)%M; 37 } 38 } 39 40 ll C(int n,int m){ 41 return fac[n]*inv_fac[n-m]%M*inv_fac[m]%M; 42 } 43 44 void fun(int ans){ 45 memset(dp,0,sizeof(dp)); 46 for(int i=0;i<105;i++)dp[k][i]=0; 47 dp[k][ans]=1;//only the same is ok 48 for(int i=k-1;i>=0;i--){ 49 for(int j=0;j<=n;j++){ 50 for(int kk=0;kk<=n;kk++){ 51 int x=m+kk-j; 52 if(x%2 == 0) 53 { 54 x/=2; int y=m-x; 55 if(x>=0&&x<=kk&&y>=0&&y<=n-kk) 56 dp[i][j]=(dp[i][j]+dp[i+1][kk]*C(kk,x)%M*C(n-kk,y)%M)%M; 57 58 } 59 60 // if(x%2==0&&y%2==0&&x/2<=j&&x/2>=0&&y/2<=n-j&&y/2>=0) 61 62 } 63 64 } 65 } 66 printf("%lld\n",dp[0][0]); 67 } 68 69 int main() 70 { 71 init(); 72 int T; 73 scanf("%d",&T); 74 75 while(T--){ 76 scanf("%d%d%d",&n,&k,&m); 77 string s1,s2; 78 cin>>s1>>s2; 79 int ans=0; 80 for(int i=0;i<s1.length();i++){ 81 if(s1[i]!=s2[i])ans++; 82 } 83 fun(ans); 84 } 85 return 0; 86 }
以上是关于B - Flipping Game (组合数学+dp+思维)的主要内容,如果未能解决你的问题,请参考以下文章
[LeetCode] 822. Card Flipping Game