Problem:
Score: 100 points

Problem Statement

E869120 has A 1-yen coins and infinitely many 500-yen coins.

Determine if he can pay exactly N yen using only these coins.

Constraints

N is an integer between 1 and 10000 (inclusive).

A is an integer between 0 and 1000 (inclusive).

Input

Input is given from Standard Input in the following format:

N
A

Output

If E869120 can pay exactly N yen using only his 1-yen and 500-yen coins, print Yes; otherwise, print No.

Sample Input 1

2018
218

Sample Output 1

Yes

We can pay 2018 yen with four 500-yen coins and 18 1-yen coins, so the answer is Yes.

Sample Input 2

2763
0

Sample Output 2

No

When we have no 1-yen coins, we can only pay a multiple of 500 yen using only 500-yen coins. Since 2763 is not a multiple of 500, we cannot pay this amount.

Sample Input 3

37
514

Sample Output 3

Yes

Solution in Scala (2.11.7) (judge verdict: Runtime Error)

object Main extends App {
  val line = readLine.split(" ").map(_.toInt)
  val H = line(0)
  val W = line(1)
  val board = Array.ofDim[Int](H, W)
  for(j <- 0 until H) {
    val l = readLine.split(" ")
    for((b, i) <- l.zipWithIndex) {
      board(j)(i) = if(b == ".") 0 else 1
    }
  }
  board(0)(0) = 2
  board(H-1)(W-1) = 2

  def calculate(board:Array[Array[Int]]):Int = board.map(_.count(_ == 0)).sum

  def nextStep(board: Array[Array[Int]], pos:(Int, Int)=(0,0)):Int = {
    val direction = List((0,1), (0,-1), (1, 0), (-1,0))
    var maximum = -1
    for(d <- direction) {
      val nextX = pos._1 + d._1
      val nextY = pos._2 + d._2
      val m = (nextX, nextY) match {
        case (x:Int, y:Int) if x == -1 || y == -1 || x == W || y == H => -1
        case (x:Int, y:Int) if x == W-1 && y == H-1 => calculate(board)
        case (x:Int, y:Int) if board(y)(x)== 0 =>
          val copy = board.map(_.clone)
          copy(y)(x) = 2
          copy(pos._1)(pos._2) = 2
          nextStep(copy, (x, y))
        case (x:Int, y:Int) if board(y)(x)== 1 || board(y)(x) == 2 => -1
        case (x:Int, y:Int) =>
          println(f"x: $x, y:$y, board:${board(y)(x)}")
          -1
      }
      maximum = if(maximum > m) maximum else m
    }
    return maximum
  }
  val maximum = nextStep(board)
  println(maximum)
}