New Maths
Time limit1sMemory limit1024 MB
Given N, find the smallest positive integer a whose carryless square (base 10 multiplication with carries discarded) equals N, or report -1.
- Level
Medium7 of 10
- Topics
- Math, Backtracking, Implementation, Brute force
- Solved
- No attempts yet
Problem
"Drat!" cursed Charles. "This stupid carry bar is not working in my Engine! I just tried to calculate the square of a number, but it's wrong; all of the carries are lost."
"Hmm," mused Ada, "arithmetic without carries! I wonder if I can figure out what your original input was, based on the result I see on the Engine."
Carryless addition, denoted by , is the same as normal addition, except any carries are ignored (in base ). Thus, is , not .
Carryless multiplication, denoted by , is performed using the schoolboy algorithm for multiplication, column by column, but the intermediate additions are calculated using carryless addition. More formally, let be the digits of , where is its least significant digit. Similarly let be the digits of . The digits of are given by the following equation: [ c_k = \left( a_0 b_k \oplus a_1 b_{k-1} \oplus \cdots \oplus a_{k-1} b_1 \oplus a_k b_0 \right) \bmod{10}, ] where any or is considered zero if or . For example, is , is , and is .
Given , find the smallest positive integer such that .
Input
The input consists of a single line with a positive integer , with at most digits and no leading zeros.
Output
Print, on a single line, the least positive number such that . If there is no such , print '-1' instead.