Under construction

Still being built. Things may change

Bitsets

Time limit3sMemory limit2048 MB

Summary
Count the columns where the bits in rows l through r are all equal, with each query's l and r generated online from the previous answers.
Level

Medium7 of 10

Topics
Bit manipulation, Prefix sum, String
Solved
No attempts yet

Problem

Consider the following operations on bitsets of size mm:

  • c=a and bc = a\,\mathrm{and}\,b: ci=1c_i=1 if both aia_i and bib_i are 1. Otherwise, ci=0c_i=0.
  • c=a or bc = a\,\mathrm{or}\,b: ci=1c_i=1 if at least one of aia_i and bib_i is 1. Otherwise, ci=0c_i=0.
  • c=a xor bc = a\,\mathrm{xor}\,b: ci=1c_i=1 if exactly one of aia_i and bib_i is 1. Otherwise, ci=0c_i=0.
  • c=not ac = \mathrm{not}\,a: ci=1c_i=1 if aia_i is 0. Otherwise, ci=0c_i=0.

You are given an array of bitsets s1,s2,…,sns_1, s_2, \ldots, s_n. Write a program that answers kk queries of the following form:

  1. Read two integers ℓ\ell and rr.
  2. Compute the bitset t=(sℓ and sℓ+1 and … and sr) xor (not (sℓ or sℓ+1 or … or sr))t = (s_\ell \,\mathrm{and}\, s_{\ell+1} \,\mathrm{and}\, \ldots \,\mathrm{and}\, s_r) \,\mathrm{xor}\, (\mathrm{not}\, (s_\ell \,\mathrm{or}\, s_{\ell+1} \,\mathrm{or}\, \ldots \,\mathrm{or}\, s_r)).
  3. Count the number of ones in tt. This count is the answer.

Input

The first line contains two integers nn and mm (1≤n,m≤1051 \leq n, m \leq 10^5; n⋅m≤106n \cdot m \leq 10^6). Each of the following nn lines contains a bitset of mm characters, each 0 or 1.

The next line contains the number of queries kk (1≤k≤2⋅1071 \leq k \leq 2 \cdot 10^7). The following line contains three integers xx, yy, and zz (1≤x,y,z≤1091 \leq x, y, z \leq 10^9).

The queries are generated from xx, yy, zz and the answers q1,q2,…,qk−1q_1, q_2, \ldots, q_{k-1} to the previous queries. Define the sequences aa and bb as follows:

  • a1=1a_1 = 1.
  • b1=nb_1 = n.
  • For i>1i > 1, ai=(ai−1⋅x+qi−1⋅y+z) mod n+1a_i = (a_{i-1} \cdot x + q_{i-1} \cdot y + z) \bmod n + 1.
  • For i>1i > 1, bi=(bi−1⋅y+qi−1⋅z+x) mod n+1b_i = (b_{i-1} \cdot y + q_{i-1} \cdot z + x) \bmod n + 1.

For query ii, let ℓ=min⁡(ai,bi)\ell = \min(a_i, b_i) and r=max⁡(ai,bi)r = \max(a_i, b_i).

Output

Output a single integer: the sum of the answers to all queries.

Hint

The queries are listed below:

#ℓ\ellrranswer
1114411
2334433
3224422
4113333

Examples1

  1. Example 1

    Input
    4 10
    1010110101
    0101111001
    1101101101
    1011010000
    4
    10 5 4
    
    Expected output
    9