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
tarun_28: 2019-12-03 16:22:07

5 lines of code in python :)

ycdxc: 2019-08-12 08:41:50

Jisse ye ni hua usse kuch ni hoga. You suck losers.

sanket17: 2019-07-07 14:11:17

use string in c++ otherwise python is always there

upendra17: 2019-06-25 09:41:07

Game of Nim

anand_undavia: 2019-06-17 18:57:36

Problems as simple as this takes 0.14 seconds in Java to be executed :D

prashant sakerwal: 2019-06-14 12:38:44


n%10 :D

Last edit: 2019-06-14 12:39:07
bhavikapawani: 2019-06-09 06:53:35

The easiest one on spoj:)

king_rohan12: 2019-05-23 07:26:12

I didnt get the problem please anyone can explain me because output of sample testcase didnt explains mutch

hetp111: 2019-05-19 15:39:07

Short n' sweet

markaman: 2018-12-27 17:58:10


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