Two variables that sit in the same cache block can slow down two cores, even if each core uses only its own variable. Find out which pairs of addresses share a block.
What to build
A program that tells, for each pair of addresses, whether they are in the same cache block.
Requirements
- The block number of an address is the address divided by the block size, rounded down.
- For each pair print
sameif both addresses are in the same block, otherwisedifferent.
Input
A line with the block size, a line with q, then q lines a b.
Output
q lines.
Constraints
1 ≤ block size ≤ 4096; 1 ≤ q ≤ 100; addresses are from 0 to 10⁹.
Example
Input:
64
3
0 8
63 64
100 120
Output:
same
different
same
Why it works
A block is a run of block-size consecutive addresses starting at a multiple of the block size, so integer division gives each address's block number, and equal numbers mean the same block.
Examples
64 3 0 8 63 64 100 120
same different same
1 2 5 5 5 6
same different
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Two addresses are in the same cache block when a // block_size is equal for both.
Solution
Show one correct solution (try it yourself first)
block = int(input())
q = int(input())
for _ in range(q):
a, b = map(int, input().split())
print("same" if a // block == b // block else "different")
▶ Open this solution in the compiler, then press Visualize to watch it run step by step