Micro in Problem

3.5

6 votes
Medium
Problem

Micro has just joined internship in a big company. Here he has to test some problems. He has got his first problem but he has to go for his sister's marriage. So, he has asked you, a dear friend to him, to test the problem on his behalf . If you do so, he would get you a big gift and you'll definitely be getting a big party :)
The problem says:
You are given a matrix A of size m*n filled with positive integers. Now you are given q queries.
In each query:

  1. You are given four integers x1,y1,x2,y2. Let
    L1(i,j)=A(i,j)    0<=i<=(x1-1), 0<=j<=(y1-1)
    R1(i,j)=A(i,j)    (x1+1)<=i<=(m-1), (y1+1)<=j<=(n-1)
    L2(i,j)=A(i,j)    0<=i<=(x2-1), 0<=j<=(y2-1)
    R2(i,j)=A(i,j)    (x2+1)<=i<=(m-1), (y2+1)<=j<=(n-1)
    be four submatrices as defined. For each query, you have to calculate value as:
    value= (sum of all elements in overlapping region of L1 and L2) + (sum of all elements in overlapping region of R1 and R2)
  2. You are given an integer k , followed by k distinct prime numbers.

You have to count how many integers from 1 to value are divisible by any of these k primes (don't count any number twice).
Finally for each query, you have to print the value and this count space separated in one line.
See the sample input/output and image for more clarity.
Now hurry up as the deadline is coming.

Input:
First line contains two integers m and n.
Then a matrix of size m*n is given as input.
Then number of queries q.
For each query, first line contains x1,y1,x2,y2. Second line contains k. Third line contains k distinct prime numbers.

Output:
For each query,in one line, you have to print the value obtained, then after a space, print count.

Constraints:
1<=m,n<=500
1<=A(i,j)<=10^9
1<=q<=18000
0<=x1,x2<=m-1
0<=y1,y2<=n-1
1<=k<=10
k distinct prime numbers ; each prime number<=61
All the inputs are integers

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

enter image description here
In the first query,
The red matrix in the left is L1 and the red matrix in the right is R1.
The blue matrix in the left is L2 and the blue matrix in the right is R2.
overlapping region of L1 and L2 contains only one element as 1
overlapping region of R1 and R2 contains only one element as 16
value=1+16=17
primes={2,3,5}
numbers divisible by 2 in {1,2,3,....,15,16,17}={2,4,6,8,10,12,14,16}
numbers divisible by 3 in {1,2,3,....,15,16,17}={3,6,9,12,15}
numbers divisible by 5 in {1,2,3,....,15,16,17}={5,10,15}
numbers divisible any of 2,3 or 5={2,3,4,5,6,8,9,10,12,14,15,16}
so count=12
hence output: 17 12

Editor Image

?