REACHFAST - Editorial

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4

Author: utkarsh_25dec
Testers: IceKnight1093, tejas10p
Editorialist: IceKnight1093

DIFFICULTY:

777

PREREQUISITES:

None

PROBLEM:

Chef wants to move from point A to point B, each time moving a distance of at most K. How many steps are needed?

EXPLANATION:

Suppose Chef takes n steps. Then, notice that Chef can move a distance of anywhere between 0 and n\cdot K.

Now, let d = |A - B| be the distance between A and B.
Notice that we want the smallest possible n such that n\cdot K \geq d, since this is the smallest number of steps needed to move a distance of d.

Finding this n can be done by brute-force, or you can use the formula

n = \left\lceil \frac{d}{K} \right\rceil

where \left\lceil \ \right\rceil denotes the ceiling function.

TIME COMPLEXITY:

\mathcal{O}(1) per testcase.

CODE:

Code (Python)
for _ in range(int(input())):
    x, y, k = map(int, input().split())
    print((abs(y-x) + k - 1)//k)