Page number and offset

A virtual address is a page number and an offset inside that page. Split addresses for a given page size.

What to build

A program that prints the page number and the offset of each address.

Requirements

  • The page number is the address divided by the page size, rounded down, and the offset is the remainder.
  • For each address print page P offset O.

Input

A line with the page size, a line with n, then n addresses, one per line.

Output

n lines.

Constraints

1 ≤ page size ≤ 65536; 1 ≤ n ≤ 100; addresses are from 0 to 10⁹.

Example

Input:

4096
3
0
4095
4096

Output:

page 0 offset 0
page 0 offset 4095
page 1 offset 0

Why it works

A page holds page-size consecutive addresses, so integer division finds which page an address is in, and the remainder says how far into the page it is.

Examples

Example 1 · Input
4096
3
0
4095
4096
Expected output
page 0 offset 0
page 0 offset 4095
page 1 offset 0
Example 2 · Input
256
2
1000
65535
Expected output
page 3 offset 232
page 255 offset 255

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

Hints

Hint 1

Use divmod(address, page_size) to get the page number and the offset together.

Solution

Show one correct solution (try it yourself first)
size = int(input())
n = int(input())
for _ in range(n):
    a = int(input())
    p, o = divmod(a, size)
    print("page", p, "offset", o)

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Virtual memory, page tables and the TLB →