• + 0 comments

    scala:

    import scala.io.StdIn
    
    object Solution {
      private val modulo = math.pow(10, 8).toInt + 7
    
      private def catalanNumber(n: Int): BigInt = {
        if (n < 1)
          return 1
        def factorial(end: Int, start: Int = 0, current: BigInt = 1): BigInt = {
          current * ((start + 1) to end).map(BigInt.apply).product
        }
        val fn = factorial(n)
        val fn1 = factorial(n + 1, start = n, current = fn)
        val f2n = factorial(2 * n, start = n + 1, current = fn1)
        f2n / (fn1 * fn)
      }
    
      def main(args: Array[String]): Unit = {
        val in = Iterator
          .continually(StdIn.readLine())
          .takeWhile(null != _)
          .map(_.trim)
        val t = in.next().toInt
        (1 to t)
          .map(_ => in.next().toInt)
          .map(catalanNumber)
          .map(_ % modulo)
          .foreach(println)
      }
    }