【gdgzezoi】Problem C: Shuffle and Swap

Description

You have two lengths are 01 strings of N A and B. And a number equal to the number B A 1 in.

By the way you want to become the A B:

  1. Set a1, a2, ..., ak is the index for all of A 1.
  2. Set b1, b2, ..., bk is the index of all of the B 1.
  3. The a1, a2, ..., ak random order, the probability of each arrangement are appearing 1k !, This step will generate k! Different results.
  4. The b1, b2, ..., bk random order, the probability of each arrangement are appearing 1k !, This step will generate k! Different results.
  5. For all 1≤i≤k, followed by exchange Aai and Abi.

After P is referred to so engage Ends A and B become equal probability. Clearly, P × (k!) 2 is an integer, you need to calculate the value of this integer modulo 998,244,353.

Input
Input consists of two rows of a character string 01, respectively A and B.

Output
Output an integer: P × (k!) 2mod998244353 .
The Input the Sample
the Sample the Input. 1
1010
1100 is

Sample Input 2
01001
01001

Sample Input 3
101010
010101

Sample Input 4
1101011011110
0111101011101
Sample Output
Sample Output 1
3

Sample Output 2
4

Sample Output 3
36

The Output. 4 the Sample
932 171 449
the HINT
1 ≦ | A | = | B | ≤104
A, composition B by 0,1, comprising a number equal to 1, and comprising at least one 1.

40 minutes subtasks present, satisfies 1≤ | A | = | B | ≤500

Thinking

We can split the answer into two steps:

1. enumeration match a and b

2. disrupt the match order

Suppose we have completed an operation, we expect a legitimate program to calculate each matching can produce

Attempts at conversion model: For a given matching, from the aiai bibi connected to a directed edge, this figure can be found eventually consists of several rings and a plurality of strands, and the strand is the sequence unique

Suppose there are e Ai = Bi = 1Ai = Bi = 1, m one Ai = 1, Bi = 0Ai = 1, Bi = 0, number of sides can be found e + m, m FIG strand and a plurality of rings

F [i] [j] denotes the front i j assigned strands legitimate program desired points

Then metastasis: f [i] [j] = Σu≤ju = 0f [i-1] [j-u] / (u + 1)
final answer is the e * m * (e + m )!! ! * Σj≤ej = 0f [m] [j]

Code

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=20077,p=998244353;
const ll top=1e17;
int power(int a,int b)
{
    int ass=1;
    for(;b;b>>=1,a=1ll*a*a%p) if(b&1) ass=1ll*ass*a%p;
    return ass;
}
char A[N],B[N];
int fac[N],unfac[N],val[N],l,n,m,c,ass;
int C(int n,int m)
{
    return 1ll*fac[n]*unfac[m]%p*unfac[n-m]%p;
}
int work(int n,int m,int k)
{
    return 1ll*C(m,k)*fac[k]%p*fac[m-k]%p*fac[m-k]%p*C(m+n,m-k)%p;
} 
int main()
{
    fac[0]=1;
    for(int i=1; i<=N; i++) fac[i]=1ll*fac[i-1]*i%p;
    unfac[N-1]=power(fac[N-1],p-2);
    for(int i=N-1;i;i--) unfac[i-1]=1ll*unfac[i]*i%p;
    scanf("%s%s",A+1,B+1);
    l=strlen(A+1);
    for(int i=1; i<=l; i++)
    {
        if(A[i]=='1'&&B[i]=='0') n++;
        if(A[i]=='1'&&B[i]=='1') m++;
    }
    for(int i=0; i<=n; i++) val[i]=1ll*C(n,i)*power(i,n)%p;
    for(int k=0; k<=m; k++)
    {
        ll tmp=0;
        for(int i=0; i<=n; i++)
        {
            if((n-i)&1) tmp-=val[i];else tmp+=val[i];
            if(tmp<-top||tmp>top) tmp%=p;
        }
        tmp=(tmp%p+p)%p;
        ass=(ass+1ll*fac[n]*tmp%p*work(n,m,k))%p;
        for(int i=0; i<=n; i++) val[i]=1ll*val[i]*i%p;
    }
    printf("%d\n",ass);
}

Published 703 original articles · won praise 392 · Views 140,000 +

Guess you like

Origin blog.csdn.net/Eric1561759334/article/details/100786874