Number Tower and Queries
InterviewTime limit2sMemory limit1024 MB
Given a, d, and x, find the floor holding block number x and its position from the left on that floor, where floor i has a + (i-1)d blocks.
- Level
Medium4 of 10
- Topics
- Binary search, Math
- Solved
- No attempts yet
Problem
Uichan enjoys stacking towers of blocks with numbers on them. One day Seonu found that the towers Uichan builds follow a rule. The rule Seonu found is as follows.
- The tower Uichan builds has its top at floor , and floor has blocks.
- The leftmost block on floor is marked , and the numbers increase by moving to the right.
- The leftmost block on floor is greater than the rightmost block on floor .
- Floor has more blocks than floor .
The figure below shows the tower Uichan built from floor to floor with and .

Uichan has memorized the position of every numbered block, and he answers queries from Seonu. Each query has one form.
a d x: Given and , which floor holds the block numbered , and which position from the left on that floor is it?
Take the figure above as an example. If , , and , Uichan answers . This means the block numbered is the 3rd number on floor 4.
Input
The first line contains the number of queries that Seonu asks Uichan.
The next lines each contain , , and , separated by spaces.
All values in the input are integers.
Output
For each of the lines, print the floor where the block numbered is located, and its position counted from the leftmost block on that floor.