Problem:
Score : 300 points

Problem Statement

You are given two non-negative integers L and R.
We will choose two integers i and j such that L \leq i < j \leq R.
Find the minimum possible value of (i \times j) \mbox{ mod } 2019.

Constraints

All values in input are integers.

0 \leq L < R \leq 2 \times 10^9

Input

Input is given from Standard Input in the following format:

L R

Output

Print the minimum possible value of (i \times j) \mbox{ mod } 2019 when i and j are chosen under the given condition.

Sample Input 1

2020 2040

Sample Output 1

2

When (i, j) = (2020, 2021), (i \times j) \mbox{ mod } 2019 = 2.

Sample Input 2

4 5

Sample Output 2

20

We have only one choice: (i, j) = (4, 5).

Solution in Scala (2.11.7) (judge verdict: Accepted)

import java.util.Scanner

import scala.annotation.tailrec
import scala.collection.mutable.ArrayBuffer
import scala.util.Random


object Main {
  def read() = {
    val sc = new Scanner(System.in)
    val l, r = sc.nextInt()
    (l, r)
  }

  def solve(l: Int, r: Int): Long = {
    val mod = 2019
    // 0~2018が必要条件
    val ml = l % mod
    val mr = r % mod
    // ml <= mr の場合: [ml, mr]の範囲
    // ml > mrの場合: [0, mr], [ml, 2018]

    if (mr < ml) return 0
    val dl = l / mod
    val dr = r / mod

    val range: IndexedSeq[Int] = ml to mr
    if (dl == dr) {
      (for (i <- range; j <- range; if i < j) yield {
        i * j % mod
      }).min
    } else {
      (for (i <- range; j <- range) yield {
        i * j % mod
      }).min
    }
  }

  def solve2(l: Int, r: Int): Long = {
    (for (i1 <- l until r; i2 <- l + 1 to r if i1 < i2) yield {
      i1 * i2 % 2019
    }).min
  }

  def solve3(l: Int, r: Int): Long = {
    val mod = 2019
    // 0~2018が必要条件
    val ml = l % mod
    val mr = r % mod
    // ml <= mr の場合: [ml, mr]の範囲
    // ml > mrの場合: [0, mr], [ml, 2018]

    val dl = l / mod
    val dr = r / mod
    if (dl < dr) return 0

    val range: IndexedSeq[Int] = ml to mr
    (for (i <- range; j <- range; if i < j) yield {
      i * j % mod
    }).min
  }

def readRand () = {
  val l = 1000
  val r = Random.nextInt (4000) + l + 1
  (l, r)
}

  def test (): Unit = {
  val (l, r) = readRand ()
  println (l, r)
  println (solve (l, r) == solve2 (l, r) )
}


  def main (args: Array[String] ): Unit = {
  //test()
  val (l, r) = read ()
  println (solve3 (l, r) )
}
}