Count the Steps
All submissions for this problem are available.
In a library, there are n racks. Each rack contains k books. The books are numbered from 0 to (n*k)-1. There is a ladder to reach all the racks except the bottom-most rack.
You need to find out the number of steps the chef needs to climb in the ladder to get all the books, where one step is when the chef reaches from one rack to the next higher rack or the next lower rack. The chef must be in the ground floor after retrieving all the books.
For example, if there are 3 racks. So, n is 3, numbered from 0 to 2.
There are 2 books in each rack. So, k is 2.
The books are numbered from 0 to (n*k)-1 that is 5.
The ladder is to reach the second and third rack only. Note: There are multiple copies of each book. So, the chef can retrieve each book more than once.
First line contains single integer T denoting number of testcases.
Next line contains three integers in the order
- The number of racks n
The number of books in each rack k
The number of books to retrieve q
For each testcase T print number of steps in a newline.
3 2 5
0 2 1 3 4
2 3 4
2 3 4 5
22 42 2
|Time Limit:||1 sec|
|Source Limit:||50000 Bytes|
|Languages:||C, CPP14, JAVA, PYTH, PYTH 3.6|
Fetching successful submissions