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 Ruby (2.3.3) (judge verdict: Runtime Error)

require 'pp'

@h, @w = gets.strip.split.map(&:to_i)
maze = []

@h.times do
  maze << gets.strip.split('')
end

# 元の白マスの個数
# pp maze
num_whites = 0
@h.times do |i|
  @w.times do |j|
    # puts "maze[i][j] = #{maze[i][j]}"
    if maze[i][j] == '.'
      num_whites += 1
    end
  end
end

def bfs(maze)
  q = [[0, 0]]
  path = []
  @h.times do |i|
    path << []
    @w.times do |j|
      path[i] << nil
    end
  end

  until q.empty? do
    now_x, now_y = q.shift

    # maze.each do |row|
    #   puts row.join('')
    # end
    # 次のマスに到達した
    # ここはゴールか？
    if now_x == @w - 1 && now_y == @h - 1
      break
    end

    dirs = [
      [now_x - 1, now_y],
      [now_x + 1, now_y],
      [now_x, now_y - 1],
      [now_x, now_y + 1]
    ]
    dirs.each do |d|
      x, y = d
      if x < 0 || @w <= x || y < 0 || @h <= y
        next
      end
      cell = maze[y][x]
      if cell == '#'
        next
      end

      # 進めるマスがあった
      # puts "すすめるマスがあった(x, y) = #{x}, #{y}"

      # チェック済みにする
      maze[y][x] = '#'

      # 経路復元用のメモ
      if path[y][x] == nil
        path[y][x] = [now_x, now_y]
      end

      # キューに入れる
      q << [x, y]
    end
  end

  # 経路を返して探索終わり
  path
end

def path_length(path)
  len = 0
  x, y = @w - 1, @h - 1
  until (x == 0 && y == 0)
    # puts ("x, y = #{x}, #{y}")
    len += 1
    x, y = path[y][x]
  end
  len
end

path = bfs(maze)
# pp path
if path[@h - 1][@w - 1] == nil
  # ゴールにたどり着けない
  puts -1
else
  # puts "num_whites = #{num_whites}"
  # 元の白マスの個数 - 経路の長さ - 1 個塗って良い
  puts num_whites - path_length(path) - 1
end