This page is still under construction.

Parts of this page are still being built. What you see may change.

Cuckoo for Hashing

Time limit2sMemory limit128 MB

Summary
Simulate cuckoo hashing insertions into two mod-indexed tables with displacement and print the final table contents.
Level

Easy2 of 10

Topics
Simulation, Hash map, Array
Solved
No attempts yet

Problem

An integer hash table is a data structure that supports insert, delete and lookup of integer values in constant time. A traditional hash structure consists of an array of size nn (the hash table) and a hash function f(x)f(x), which is usually f(x)=x mod nf(x) = x \bmod n. To insert a value xx into the table, you compute its hash value f(x)f(x), which is the index of the location where xx goes. For example, if x=1234x = 1234 and the hash table has size 101101, then 12341234 goes into location 22=1234 mod 10122 = 1234 \bmod 101. Of course, some other value may already sit in that location (x=22x = 22, for example), which is a collision. Collisions can be handled in several ways, none of which this problem deals with.

Cuckoo hashing uses two hash tables T1T_1 and T2T_2, each with its own hash function f1(x)f_1(x) and f2(x)f_2(x). Insertion of a value xx proceeds as follows. You first try to store xx in T1T_1 at location f1(x)f_1(x). If that location is empty, store xx there and the insertion is done. Otherwise there is a collision to handle. Let yy be the value currently in that location. You replace yy with xx in T1T_1, then try to store yy in T2T_2 at location f2(y)f_2(y). Again, if that location is empty, you store yy there and the insertion is done. Otherwise you replace the value there (call it zz) with yy, and try to store zz back in T1T_1 at location f1(z)f_1(z), and so on. The process bounces back and forth between the two tables until an empty location turns up. A real implementation rehashes both tables once a certain number of swaps have occurred, but that never happens in this problem: every insertion is guaranteed to find an empty location.

Given the sizes of the two tables and a series of insertions, determine what each table holds at the end.

The name comes from the cuckoo bird, which lays its eggs in another bird's nest. The larger cuckoo chick hatches first and pushes the other chicks out of the nest, so it gets all the food. Gruesome but efficient.

Input

The input holds several test cases. Each test case starts with three positive integers n1n_1, n2n_2 and mm, where n1n_1 and n2n_2 are the sizes of tables T1T_1 and T2T_2, and mm is the number of insertions. The mm values to insert follow, in insertion order, and all of them are non-negative. Both tables start empty, and table TiT_i uses the hash function fi(x)=x mod nif_i(x) = x \bmod n_i. A line containing three zeros ends the input and is not a test case.

Constraints: 1≤n1,n2≤10001 \le n_1, n_2 \le 1000, n1≠n2n_1 \ne n_2

Output

For each test case, print Case k: on its own line, where kk counts the test cases starting at 1. Then, if T1T_1 holds at least one value, print Table 1 on its own line, followed by the non-empty locations of T1T_1 from the lowest index to the highest, one per line, in the form i:v, where ii is the index of the location and vv is the value stored there. Print Table 2 and the non-empty locations of T2T_2 the same way. If a table is empty, print nothing for it, not even its header.

Examples1

  1. Example 1

    Input
    5 7 4
    8 18 29 4
    6 7 4
    8 18 29 4
    1000 999 2
    1000
    2000
    0 0 0
    
    Expected output
    Case 1:
    Table 1
    3:8
    4:4
    Table 2
    1:29
    4:18
    Case 2:
    Table 1
    0:18
    2:8
    4:4
    5:29
    Case 3:
    Table 1
    0:2000
    Table 2
    1:1000