TAP2015H - Hugo s homework

no tags 

[Due to SPOJ restrictions, this problem has been modified with respect to the original version used in the Argentinian Programming Tournament of 2015 in order to have multiple test cases per input file. The original version of this problem (in Spanish) can be found at http://torneoprogramacion.com.ar/wp-content/uploads/2015/09/pruebaTAP2015.pdf ]

Hugo goes to primary school, but he is convinced that not enough homework is being given to him. Specifically, he was recently taught how to subtract two numbers, but each day he is given a single subtraction to perform at home. Hugo knows that in order to master such a complex technique he should practice much more, so he has decided to take matters into his own hands and create his own homework.

It's not easy for Hugo to invent exercises about a subject he does not fully comprehend, so he has devised the following method to perform multiple subtractions. He starts by asking his mother for a number N, and then forms the number M containing the same digits as N in increasing order from left to right, after which he finally performs the subtraction N-M. For example, if his mother chooses the number N = 321 then M = 123 and the subtraction Hugo must perform is N - M = 321-123 = 198.

Hugo wouldn't want to bother his mother too often, so he will repeat this procedure using the result of the subtraction N-M in one step as the number N he starts with in the next step. This will end only when at some point he reaches the value N = 0, as this case is useless to practice subtractions because he would have M = 0: Hugo already knows perfectly well that if he has no candies he cannot eat any candies, and will therefore continue to not have or eat candies forever.

Now Hugo's mother would like to know, given a number N, how many subtractions Hugo can perform if she gives him that number to start his homework. In the previous example, in the second step Hugo would have N = 198 so that M = 189 and N - M = 198 - 189 = 9. Then in the third step N = 9, M = 9 and N - M = 0, so here the fun ends because in the fourth step he would have N = 0. Thus, starting with the number N = 321 Hugo will perform 3 subtractions.



There are multiple test cases in the input file. Each test case consists of one line containing an integer N, representing the number Hugo's mother will give him to start his homework (1 ≤ N ≤ 109).



For each test case, print one line containing one integer representing the number of subtractions Hugo will perform if he starts his homework with number N.





hide comments
viratian_070: 2017-07-07 07:36:55

please check submission for id=19749123..logic is correct but still showing tle...plz check

aditya_rev: 2017-06-21 23:29:21

priority queue make it more easier :D

madhavgaba: 2016-07-14 18:03:01

for input use while(scanf("%lld",&num)==1)

iloveaakanksha: 2016-06-15 09:21:48

Finally a century on ProjectEuler , CodeForces , HackerRank , HackerEarth and SPOJ
Just a large problem :-(

vivekrnj: 2016-03-31 19:50:32

nothing here.....just large prob.!!!

Utkarsh Ruhela: 2015-12-30 11:21:12

How to end the program?

Devashish Mathur: 2015-12-25 10:35:36

cakewalk... beware of border case

Mihajlo: 2015-11-28 10:55:04

ez bf :D

Fidel Schaposnik: 2015-11-02 20:10:03

step 1) 20 - 02 = 18
step 2) 18 - 18 = 0
Then the answer is 2...

Harish Meena: 2015-10-31 23:36:19

EOF still the worst way to end the code

Added by:Fidel Schaposnik
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64 GOSU JS-MONKEY
Resource:Argentinian Programming Tournament 2015