#P9915. 「RiOI-03」3-2
「RiOI-03」3-2
Background
Heart beat to death.
Problem Description
Given a positive integer . Write the lowest bits of every integer in in binary, from low to high, into a matrix in order. Both dimensions of the matrix are indexed starting from . For example, when , the matrix looks like this:

Given queries. For each query, ask for the size of the 4-connected component containing the cell at index in this matrix, modulo .
Input Format
The first line contains two positive integers .
The next lines each contain two non-negative integers , representing one query.
Output Format
Output lines, each containing one positive integer, which is the answer for each query modulo .
2 2
1 1
2 0
3
1
Hint
Sample #1 Explanation

The figure shows the matrix when , where cells with the same color form a 4-connected component.
Constraints
This problem uses bundled testdata.
| Score | |||
|---|---|---|---|
For of the testdata, , , .
Please use fast input/output methods.
Translated by ChatGPT 5