B. Another Divisibility Problem

B. Another Divisibility Problem

Problem

Alice and Bob are playing a game. Alice gives Bob a positive integer xx.

To win, Bob must find a positive integer yy such that the number formed by concatenating xx and yy (denoted as x#yx \# y) is divisible by x+yx + y.

For example, if x=835x = 835 and y=47y = 47, then x#y=83547x \# y = 83547 (the number formed by writing 835 followed by 47).

It can be proven that such a yy always exists. Help Bob find one such yy.

Input

  • One integer xx (1x<1081 \leq x < 10^8)

Output

  • One integer yy (1y<1091 \leq y < 10^9) such that x#yx \# y is divisible by x+yx + y

Solution

Key Observations

  1. For a number xx with dd digits, concatenating yy to xx is equivalent to computing:
    • x#y=x×10digits(y)+yx \# y = x \times 10^{\text{digits}(y)} + y
  2. We need to find yy such that (x#y)mod(x+y)=0(x \# y) \bmod (x + y) = 0
  3. One strategy is to try y=kxy = kx for some integer kk, which makes x+y=(k+1)xx + y = (k+1)x

Solution Strategy

Set y=2xy = 2x

  • Then x+y=3xx + y = 3x
  • And x#yx \# y is xx concatenated with 2x2x
  • This often works and gives a smaller answer

See my submission