Chef and Number
All submissions for this problem are available.Chef has provided you a number N and a value K, and he wants to reduce N to 1 by using following operations ( any number of times ) - N can be incremented by 1 - N can be decremented by 1 - N can be reduce to N/K ( if N is dividible by K ) Help chef to find out the minimum number of operations to be performed to reach 1 ? ###Input: - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, two integers $N, K$. ###Output: For each testcase, output in a single line integer minimum number of opeartions. ###Constraints - $1 \leq T \leq 1000$ - $2 \leq N \leq 10^5$ - $1 \leq K \leq N$ ###Sample Input: 1 7 2 ###Sample Output: 4 ###EXPLANATION: - 7 decremented by 1 = 6 ( operation 1 ) - 6 reduces by 2 = 3 ( operation 2) - 3 decremented by 1 = 2 ( operation 3) - 2 decremented by 1 = 1 ( operation 4 ) - Number of operations = 4. Hence the answer is 4. .
|Time Limit:||1 sec|
|Source Limit:||50000 Bytes|
|Languages:||C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, rust, SCALA, swift, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, SQL, kotlin, PERL6, TEXT, SCM chicken, PYP3, CLOJ, R, COB, FS|
Fetching successful submissions
If you are still having problems, see a sample solution here.