kirjuri's blog

By kirjuri, 10 years ago, In English

Is this problem solvable with some data structure or some other idea is required?

You have M sets of N-bit strings, numbered from 1 to M, and all of them are initially empty. Then, you have Q operations of the following kind: add to the i-th set, all the N-bit strings with the k-th bit equal to b. You must output the number of N-bit strings in the set consisting of the intersection of all the M sets after adding all the strings.

Input

The first line of input consists of 3 positive integers: M (2 ≤ M ≤ 100000), N (1 ≤ N ≤ 10000) and Q (0 ≤ Q ≤ 100000). The next Q lines consists each of 3 integers: i (1 ≤ i ≤ M), k (0 ≤ k < N) and b (), representing the operation described above in the statement.

Output

One line with one integer. The number of strings in the intersection of the M sets after all the operations. It is guaranteed that the answer will fit in a 32 bit signed integer.

Example input

3 3 3
1 0 1
3 2 1
2 1 0

Example output

1

Explanation

There are 3 initial empty sets of 3-bit strings. There are 3 operations. The first one adds to the first set all the strings with the bit 0 equal to 1. The second one adds to the third set all the strings with the bit 2 equal to 1. The third one adds to the second set all the strings with the bit 1 equal to 0. So, after all the operations, the sets consists of the following strings:

1) {100, 101, 110, 111}

2) {000, 001, 100, 101}

3) {001, 011, 101, 111}

The intersection of the 3 sets is the set {101}, so the answer is 1.

Thanks!

  • Vote: I like it
  • +8
  • Vote: I do not like it

»
10 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

EDITed

  • »
    »
    10 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Thanks for your answer. Can you elaborate a bit? I don't understand exactly what you want to do.

    • »
      »
      »
      10 years ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      Edited

      • »
        »
        »
        »
        10 years ago, # ^ |
        Rev. 2   Vote: I like it 0 Vote: I do not like it

        Mmm, it seems like I'm misunderstanding something in your solution. As far as I can see, your algorithm always output a power of two (or zero). Can you illustrate the process in the following case?

        2 3 4
        1 0 1
        1 1 1
        2 0 1
        2 1 1
        

        The answer is 6, but I don't see how you reach that with your algorithm. Thanks.

        P.S.: I don't have a link for the problem. It's from a local contest. Sorry about that.

        • »
          »
          »
          »
          »
          10 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          sorry, i'm misunderstand :(

»
10 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Where did you find this problem? It seems exactly like SAT to me. To see this, suppose we are given a boolean formula in CNF.

For each clause j, add a set j. Then for each literal in the clause, perform the described operation. That is, if it is of the form 'x_i', perform the operation 'set j must contain all bitstrings with i'th bit set to 1', if it is of the form 'not x_i', perform the operation 'set j must contain all bitstrings with i'th bit set to 0'. Clearly, in the end the set j will contain all bitstrings representing an assignment that would make the boolean formula evaluate to true.

Thus, if we take the intersection of all sets, any bitstring contained in it will represent an assignment that makes all clauses evaluate to true, and thus the formula is satisfiable if and only if the intersection is non-empty. So the problem is NP-Complete

In other words, if you can solve this problem in polynomial time (and the constraints look like the algorithm should be polynomial), you'll have to conclude that P = NP.

EDIT: It is in fact exactly SAT, the conversion works in the other direction as well. Bit positions correspond to variables, their values (0 and 1) to assignments (true and false) and sets correspond to clauses.