

Time Limit: 2 sec / Memory Limit: 512 MB
配点 : 点
問題文
と からなる同じ長さの二つの文字列 と があります。 に含まれる の個数は等しいです。
あなたは次のアルゴリズムによって を変化させることにしました。
- , , ..., を、 で が出現する位置の添字とする。
- , , ..., を、 で が出現する位置の添字とする。
- の要素をそれぞれ無作為に並び替える。これらの無作為な並び替えは一様かつ独立である。
- から までの各 に対して、順に と の値を入れ替える。
この手順のあと、文字列 が一致する確率を とします。
さらに、 とします。明らかに、 は整数です。
を で割った余りを求めてください。
制約
- は と からなる。
- に含まれる の個数は等しい。
- には が少なくとも一つ含まれる。
部分点
- を満たすデータセットに正解すると、 点が与えられる。
入力
入力は以下の形式で標準入力から与えられる。
出力
を で割った余りを出力せよ。
入力例 1Copy
1010 1100
出力例 1Copy
3
最初の二つのステップで、, となります。 の要素を無作為に並び替えたあとの結果としてありうるものは次の つです。
- , 。はじめ 。 と の入れ替え後 。 と の入れ替え後 。
- , 。はじめ 。 と の入れ替え後 。 と の入れ替え後 。
- , 。はじめ 。 と の入れ替え後 。 と の入れ替え後 。
- , 。はじめ 。 と の入れ替え後 。 と の入れ替え後 。
この つの結果のうち、 つで となっています。よって、 / であり、 となります。
入力例 2Copy
01001 01001
出力例 2Copy
4
の要素の入れ替えによって が変化することはなく、したがって必ず となります。
入力例 3Copy
101010 010101
出力例 3Copy
36
三回の の要素の入れ替えがどのように起こっても、 に含まれる は適切な位置に移動します。
入力例 4Copy
1101011011110 0111101011101
出力例 4Copy
932171449
Score : points
Problem Statement
You have two strings and of the same length consisting of and . The number of 's in and is equal.
You've decided to transform using the following algorithm:
- Let , , ..., be the indices of 's in .
- Let , , ..., be the indices of 's in .
- Replace and with their random permutations, chosen independently and uniformly.
- For each from to , in order, swap and .
Let be the probability that strings and become equal after the procedure above.
Let . Clearly, is an integer.
Find modulo .
Constraints
- and consist of and .
- and contain the same number of 's.
- and contain at least one .
Partial Score
- points will be awarded for passing the testset satisfying .
Input
Input is given from Standard Input in the following format:
Output
Print the value of modulo .
Sample Input 1Copy
1010 1100
Sample Output 1Copy
3
After the first two steps, and . There are possible scenarios after shuffling and :
- , . Initially, . After swap(, ), . After swap(, ), .
- , . Initially, . After swap(, ), . After swap(, ), .
- , . Initially, . After swap(, ), . After swap(, ), .
- , . Initially, . After swap(, ), . After swap(, ), .
Out of scenarios, of them result in . Therefore, / , and .
Sample Input 2Copy
01001 01001
Sample Output 2Copy
4
No swap ever changes , so we'll always have .
Sample Input 3Copy
101010 010101
Sample Output 3Copy
36
Every possible sequence of three swaps puts the 's in into the right places.
Sample Input 4Copy
1101011011110 0111101011101
Sample Output 4Copy
932171449