NGM - A Game with Numbers

no tags 

Nikifor and Trofim play the following game: they write some positive integer smaller than 2,000,000,000 and take turns one after another. Nikifor is the first to make a move. The turn is made by the following rule: from the written integer any non-zero digit is subtracted, and the new integer replaces the old one on the desk. For example for integer 40534, the next move can be: 40530, 40531 or 40529. The winner is the player who writes zero on the desk.

Write a program to decide who will win if both players do their best.


The input contains the positive integer from which the game is started.


In the first line you must write 1 if Nikifor wins and 2 otherwise. If Nikifor wins then in the second line you must output the move in the first turn which guarantees victory for him. If there are many such moves then output any of them.




Author: Filimonenkov D.O.

hide comments
rastarad: 2021-11-13 21:21:59

There is a bug in the task. You don't need to provide information to output about Trofim's first move! (C#)

manurag_20: 2021-10-19 20:38:52

Basic logic building problem. Code in one if else condition only.

saurabhpthk: 2021-09-04 17:43:59

hint: in any number(n),always subtract the rightmost digit.......and remember " a non-zero digit is subtracted"
then observe it & code it..

jainnamah: 2021-06-25 14:00:43

Don't let others the comments demotivate you, if you're not able to solve an easy problem don't panic just try to think differently. For these kind of problems start top-down, construct solutions to sub problems you'll get there :)

Simes: 2021-06-11 14:35:45

@loser_404 "Observation is- non-zero digits are from 1-9". Wow! That's an amazing observation, and not at all obvious.

loser_404: 2021-06-09 16:31:12

AC in first go. Great problem for game-theory. Observation is- non-zero digits are from 1-9. So numer of possibilities is 9.

voldemort_11: 2021-06-05 21:30:50

hide comments before you attempt :)

akshitgarg09: 2021-01-15 04:02:22

Reason I didn't understand the problem: "any non-zero digit is subtracted"
Read this line carefully

varuntumbe: 2020-09-26 05:47:39

i tried to calculate total number of moves to get 0. read the question carefully.

heyviv: 2020-06-03 18:16:12

number deducted cannot be more than 9

Added by:Roman Sol
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:ZCon 2007