A direct-mapped cache

Simulate a direct-mapped cache. Each address belongs to block address ÷ block size (rounded down), and that block may only go in line block mod lines. Every line starts empty.

Input

A line with the number of lines and the block size (both powers of two, 1 to 64). A line with n (1 ≤ n ≤ 200), then a line with n addresses (whole numbers from 0 to 65535).

Output

Two lines: a string with H for each hit and M for each miss, in order, then hits and the number of hits.

Examples

Example 1 · Input
4 4
4
0 4 16 0
Expected output
MMMM
hits 0
Example 2 · Input
4 16
6
0 1 2 3 15 16
Expected output
MHHHHM
hits 4

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Keep one entry per line: the block number it holds, or None.

Hint 2

For each address: block = address // block_size, line = block % lines.

Hint 3

A hit when that line already holds that block; otherwise a miss, and the line now holds it.

Solution

Show one correct solution (try it yourself first)
lines, block = map(int, input().split())
n = int(input())
addresses = list(map(int, input().split()))
cache = [None] * lines
out = ""
for a in addresses:
    b = a // block
    line = b % lines
    if cache[line] == b:
        out += "H"
    else:
        out += "M"
        cache[line] = b
print(out)
print("hits", out.count("H"))

Lesson: Cache memory →