Binary String Randomizer

 

Problem Code: BINSTRRAND

Chef JJ is a fan of random events and calculating probabilities.

He designed a machine called the binary string randomizer (BSRand). BSRand takes a binary string A (say, of length N) as input and then generates a binary string B (of the same length N) as output using the following algorithm:

For each i from 1 to N,

  • Uniformly randomly choose an integer j between 1 and N.
  • Uniformly randomly choose an integer k between 1 and N.
  • Set Bi:=AjAk (where  represents the bitwise XOR operation)

Given a binary string A, Chef JJ passes it through BSRand T times (i.e, first he passes the binary string A through the BSRand, then he passes the resultant binary string through BSRand again, and so on. This is done T times in total). Calculate the probability that the final binary string obtained is B.

You need to print the probability of getting binary string B modulo 998244353. Formally, let M=998244353. It can be shown that the answer can be expressed as an irreducible fraction pq, where p and q are integers and q0(modM). Output pq1(modM). In other words, output the (unique) integer x which satisfies 0x<M and xqp(modM).

Input Format

  • The first line of the input contains two integers N and T - the length of the binary strings A and B and the number of times chef JJ passes the binary string through BSRand, respectively.
  • The second line contains the binary string A.
  • The third line contains the binary string B.

Output Format

Output the probability of getting binary string B from binary string A after passing it through BSRand T times, modulo 998244353.

Constraints

  • 1N100
  • 1T106

Sample Input 1 

2 1
01
11

Sample Output 1 

748683265

Sample Input 2 

2 1
00
11

Sample Output 2 

0

Sample Input 3 

2 2
11
00

Sample Output 3 

1

Explanation

Test Case 1: For i=1, we notice that the BSRand can select the following combinations of (j,k)(1,1),(1,2),(2,1),(2,2). Out of these 4 combinations, we see that only 2 of them give 1 as result. Therefore, the probability that B1 becomes 1 is 12.

For i=2, similarly the the probability that B2 becomes 1 is 12.

Therefore the probability that the final string is 11 is 1212=14=748683265(mod998244353).

Test Case 2: It can be proved that there is no way to get string 11 from 00.

Comments

Post a Comment

Popular posts from this blog

Flipkart Runway: Season 3 | 2025 | Internship

Walmart Sparkplug 2022 | 150 students will be selected for summer internship | ₹1-1.1 lakh monthly

Top-order failure leaves India in the doldrums

Software Testing Week 5 : Assignment 5 | 2022