Boolean Expressions
Practice
4.4 (14 votes)
Approved
Combinatorics
Dynamic programming
Math
Medium
Open
Two dimensional
Problem
68% Success 1452 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Roy is intrigued by the fact that the evaluated value of boolean expressions can easily vary depending upon the order of evaluation !

For instance, consider the example:

Expression: 1 xor 1 and 0

We can have following two interpretations:

1.  ((1 xor 1) and 0) => (0 and 0) => 0
2.  (1 xor (1 and 0)) => (1 xor 0) => 1

Now, he is further interested into finding the number of possible different parenthesizations such that the result of computation is res.

Input:

The first line of input file contains two space-separated strings. The first string denotes the literals of our boolean expression S, the second string denotes the operators. The next line denotes an integer q, i.e. the number of queries to follow. Each of the next q lines contain two space separated integers l and r and a string res, which is either true or false.

Output:

For each query. output in a new line, the number of ways in which the boolean expression of substring [l,r] can be parenthesized so that it evaluates to res. As the output can be very large, please print the answer modulo 1000000009.

Constraints:

1 <= |S| <= 300
1 <= q <= 90000
1 <= l <= r <= |S|

Notes:

  • No order of precedence is to be considered. The expression should be fully parenthesized.
  • Two parenthesizations are considered different if the parenthesized string produced is different.
  • The string of operations shall contain only characters 'a', 'o' and 'x' representing and, or and xor operations respectively

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
13 votes
Tags:
2-D ArrayAlgorithmsApprovedDynamic ProgrammingDynamic programmingMedium
Points:30
8 votes
Tags:
ApprovedMathMediumOpen
Points:30
7 votes
Tags:
AlgorithmsApprovedDynamic ProgrammingGreedy AlgorithmsMediumOpenSortingTwo dimensional