E - Shuffle and Swap Editorial /

Time Limit: 2 sec / Memory Limit: 512 MB

配点 : 17001700

問題文

0011 からなる同じ長さの二つの文字列 A=A1A2...AnA = A_1 A_2 ... A_nB=B1B2...BnB = B_1 B_2 ... B_n があります。 A,BA, B に含まれる 11 の個数は等しいです。

あなたは次のアルゴリズムによって AA を変化させることにしました。

  • a1a_1, a2a_2, ..., aka_k を、AA11 が出現する位置の添字とする。
  • b1b_1, b2b_2, ..., bkb_k を、BB11 が出現する位置の添字とする。
  • a,ba, b の要素をそれぞれ無作為に並び替える。これらの無作為な並び替えは一様かつ独立である。
  • 11 から kk までの各 ii に対して、順に AaiA_{a_i}AbiA_{b_i} の値を入れ替える。

この手順のあと、文字列 A,BA, B が一致する確率を PP とします。

さらに、Z=P×(k!)2Z = P \times (k!)^2 とします。明らかに、ZZ は整数です。

ZZ998244353998244353 で割った余りを求めてください。

制約

  • 1A=B10,0001 \leq |A| = |B| \leq 10,000
  • A,BA, B0011 からなる。
  • A,BA, B に含まれる 11 の個数は等しい。
  • A,BA, B には 11 が少なくとも一つ含まれる。

部分点

  • 1A=B5001 \leq |A| = |B| \leq 500 を満たすデータセットに正解すると、12001200 点が与えられる。

入力

入力は以下の形式で標準入力から与えられる。

AA
BB

出力

ZZ998244353998244353 で割った余りを出力せよ。


入力例 1Copy

Copy
1010
1100

出力例 1Copy

Copy
3

最初の二つのステップで、a=[1,3]a = [1, 3], b=[1,2]b = [1, 2] となります。a,ba, b の要素を無作為に並び替えたあとの結果としてありうるものは次の 44 つです。

  • a=[1,3]a = [1, 3], b=[1,2]b = [1, 2]。はじめ A=1010A = 1010A1A_1A1A_1 の入れ替え後 A=1010A = 1010A3A_3A2A_2 の入れ替え後 A=1100A = 1100
  • a=[1,3]a = [1, 3], b=[2,1]b = [2, 1]。はじめ A=1010A = 1010A1A_1A2A_2 の入れ替え後 A=0110A = 0110A3A_3A1A_1 の入れ替え後 A=1100A = 1100
  • a=[3,1]a = [3, 1], b=[1,2]b = [1, 2]。はじめ A=1010A = 1010A3A_3A1A_1 の入れ替え後 A=1010A = 1010A1A_1A2A_2 の入れ替え後 A=0110A = 0110
  • a=[3,1]a = [3, 1], b=[2,1]b = [2, 1]。はじめ A=1010A = 1010A3A_3A2A_2 の入れ替え後 A=1100A = 1100A1A_1A1A_1 の入れ替え後 A=1100A = 1100

この 44 つの結果のうち、33 つで A=BA = B となっています。よって、P=3P = 3 / 44 であり、Z=3Z = 3 となります。


入力例 2Copy

Copy
01001
01001

出力例 2Copy

Copy
4

AA の要素の入れ替えによって AA が変化することはなく、したがって必ず A=BA = B となります。


入力例 3Copy

Copy
101010
010101

出力例 3Copy

Copy
36

三回の AA の要素の入れ替えがどのように起こっても、AA に含まれる 11 は適切な位置に移動します。


入力例 4Copy

Copy
1101011011110
0111101011101

出力例 4Copy

Copy
932171449

Score : 17001700 points

Problem Statement

You have two strings A=A1A2...AnA = A_1 A_2 ... A_n and B=B1B2...BnB = B_1 B_2 ... B_n of the same length consisting of 00 and 11. The number of 11's in AA and BB is equal.

You've decided to transform AA using the following algorithm:

  • Let a1a_1, a2a_2, ..., aka_k be the indices of 11's in AA.
  • Let b1b_1, b2b_2, ..., bkb_k be the indices of 11's in BB.
  • Replace aa and bb with their random permutations, chosen independently and uniformly.
  • For each ii from 11 to kk, in order, swap AaiA_{a_i} and AbiA_{b_i}.

Let PP be the probability that strings AA and BB become equal after the procedure above.

Let Z=P×(k!)2Z = P \times (k!)^2. Clearly, ZZ is an integer.

Find ZZ modulo 998244353998244353.

Constraints

  • 1A=B10,0001 \leq |A| = |B| \leq 10,000
  • AA and BB consist of 00 and 11.
  • AA and BB contain the same number of 11's.
  • AA and BB contain at least one 11.

Partial Score

  • 12001200 points will be awarded for passing the testset satisfying 1A=B5001 \leq |A| = |B| \leq 500.

Input

Input is given from Standard Input in the following format:

AA
BB

Output

Print the value of ZZ modulo 998244353998244353.


Sample Input 1Copy

Copy
1010
1100

Sample Output 1Copy

Copy
3

After the first two steps, a=[1,3]a = [1, 3] and b=[1,2]b = [1, 2]. There are 44 possible scenarios after shuffling aa and bb:

  • a=[1,3]a = [1, 3], b=[1,2]b = [1, 2]. Initially, A=1010A = 1010. After swap(A1A_1, A1A_1), A=1010A = 1010. After swap(A3A_3, A2A_2), A=1100A = 1100.
  • a=[1,3]a = [1, 3], b=[2,1]b = [2, 1]. Initially, A=1010A = 1010. After swap(A1A_1, A2A_2), A=0110A = 0110. After swap(A3A_3, A1A_1), A=1100A = 1100.
  • a=[3,1]a = [3, 1], b=[1,2]b = [1, 2]. Initially, A=1010A = 1010. After swap(A3A_3, A1A_1), A=1010A = 1010. After swap(A1A_1, A2A_2), A=0110A = 0110.
  • a=[3,1]a = [3, 1], b=[2,1]b = [2, 1]. Initially, A=1010A = 1010. After swap(A3A_3, A2A_2), A=1100A = 1100. After swap(A1A_1, A1A_1), A=1100A = 1100.

Out of 44 scenarios, 33 of them result in A=BA = B. Therefore, P=3P = 3 / 44, and Z=3Z = 3.


Sample Input 2Copy

Copy
01001
01001

Sample Output 2Copy

Copy
4

No swap ever changes AA, so we'll always have A=BA = B.


Sample Input 3Copy

Copy
101010
010101

Sample Output 3Copy

Copy
36

Every possible sequence of three swaps puts the 11's in AA into the right places.


Sample Input 4Copy

Copy
1101011011110
0111101011101

Sample Output 4Copy

Copy
932171449


2025-03-18 (Tue)
05:39:58 +00:00