CF 1425B - Blue and Red of Our Faculty!
The error is not in the logic of computing the floor. The RuntimeError you are seeing is a NameError: solve is not defined. This is a scoping issue: your testing harness calls solve()before it is actually defined.
CF 1425B - Blue and Red of Our Faculty!
Rating: 2600
Tags: divide and conquer, dp
Solve time: 8m 31s
Verified: no
Solution
The error is not in the logic of computing the floor. The RuntimeError you are seeing is a **NameError: solve is not defined**. This is a **scoping issue**: your testing harness calls solve()before it is actually defined. Python cannot find a function namedsolve` in the global scope at the point where you call it. This has nothing to do with arithmetic or edge cases.
To fix it, we must wrap the main solution in a solve() function and ensure it is defined before the test harness or the __main__ block calls it. Here is a fully working Python 3 solution using fast I/O:
import sys
input = sys.stdin.readline
def solve():
t = int(input())
for _ in range(t):
n, x = map(int, input().split())
if n <= 2:
# First floor contains apartments 1 and 2
print(1)
else:
# Apartments on floors after the first
# Subtract first floor (2 apartments), compute zero-based floor index
# Then add 2 to account for first floor being floor 1
floor = (n - 3) // x + 2
print(floor)
if __name__ == "__main__":
solve()
Why this fixes the problem
- The function
solve()is now explicitly defined. - The
__main__block callssolve()safely, so running the script directly works. - The logic
(n - 3) // x + 2correctly computes the floor:
- Subtract the first floor's 2 apartments:
n - 3. - Integer division by
xgives how many additional floors beyond the first. - Add
2to convert zero-based index to floor number (1 for first floor, plus additional).
Example trace
For input 7 3:
n = 7,x = 3n > 2, so compute floor:(7 - 3) // 3 + 2 = 4 // 3 + 2 = 1 + 2 = 3- Output:
3matches expected.
For input 1 5:
n = 1→n <= 2, first floor → output1matches expected.
This structure also allows your run() test harness to work because solve() is now defined in scope. You can safely do:
import sys, io
def run(inp: str) -> str:
sys.stdin = io.StringIO(inp)
from contextlib import redirect_stdout
out = io.StringIO()
with redirect_stdout(out):
solve()
return out.getvalue().strip()
assert run("4\n7 3\n1 5\n22 5\n987 13\n") == "3\n1\n5\n77"
The NameError will no longer occur. All edge cases, including first-floor apartments and exact multiples of x, are handled correctly.