Powers of Three Walk
InterviewTime limit2sMemory limit512 MB
Given a target point, decide whether it is reachable if stage k moves exactly 3^k in one of the four axis directions.
- Level
Medium5 of 10
- Topics
- Math, Number theory, Bit manipulation
- Solved
- No attempts yet
Problem
Donghyeok stands at the origin of a plane of infinite size.
He moves in numbered stages and wants to arrive at the point . Stage numbers start at and grow by .
At stage he picks one of four directions, right ( increases), left ( decreases), up ( increases), or down ( decreases), and then moves exactly in that direction. He cannot skip a stage.
He performs as many stages as he wants and then stops. Stopping at the origin without performing a single stage is allowed.
Given and , write a program that decides whether can be reached from .
Input
The first line contains two integers and separated by a space. ()
Output
Print if can be reached from , and otherwise.
If and are both , he is already there before any stage, so print .