F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

Periodical Cicadas

Time Limit: 14000/7000 MS (Java/Others)    Memory Limit: 80000/80000 K (Java/Others)
Total Submission(s): 692    Accepted Submission(s): 113


Problem Description
Nearly all cicadas spend years underground as juveniles, before emerging above ground for a short adult stage of several weeks to a few months.
The seven periodical cicada species are so named because, in any one location, all of the members of the population are developmentally synchronized they emerge as adults all at once every seven years.
The lifecycles of most periodical cicada species range from two to seventeen years, although some could be longer.
There is a forest which can be roughly divided into a matrix of size N ¡ÁM. The upper-left region is (1, 1) and the lower-right region is (N, M).
A population of periodical cicadas live within each region of the matrix. The population in region (i, j) emerged in year $a_{i,j}$ for the first time, and re-emerges every $b_{i,j}$ years. i.e. they are $b_{i,j}$ - periodical cicadas.
Given a selected rectangular area, entomologists wonder if there is a time when all cicadas in that area emerge together.
 

Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test cases begin with two integers N and M.
The following N lines each consists of M integers $a_{i,j}$ representing the year cicadas emerged in region (i, j) for the first time.
The following N more lines each consists of M integers $b_{i,j}$ representing the periodical cycle of the cicadas in that region.
Then comes a line with an integer Q representing the number of queries. The following Q lines each consists of 4 integers: $x_1, y_1, x_2, y_2,$ representing the upper-left and lower-right coordinate of the selected rectangular area.
 

Output
For each test case, first output one line containing ¡°Case #x:¡±, where x is the test case number (starting from 1).
The following Q lines each consists of an integer which is the year when all cicadas in the selected area emerge together for the first time or -1 if it¡¯s impossible.

limits


$\bullet 1 ¡Ü T ¡Ü 10.$
$\bullet 1 ¡Ü N, M ¡Ü 200.$
$\bullet 0 ¡Ü a_{i,j} < b_{i,j} ¡Ü 40.$
$\bullet 1 ¡Ü x_1 ¡Ü x_2 ¡Ü N.$
$\bullet 1 ¡Ü y_1 ¡Ü y_2 ¡Ü M.$
$\bullet$ 1 ¡Ü Q ¡Ü 500000.
 

Sample Input
2 3 4 3 1 1 2 1 1 2 1 1 0 5 5 5 4 2 3 2 2 3 2 4 2 6 6 5 2 2 2 2 1 1 3 4 1 4 2 4 1 1 1 2 2 2 3 4 1 2 0 1 2 2 1 1 1 1 2
 

Sample Output
Case #1: 1 -1 5 13 -1 Case #2: -1
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-11-22 09:33:57, Gzip enabled