An array of integers $$$p_1, p_2, \dots, p_n$$$ is called a permutation if it contains each number from $$$1$$$ to $$$n$$$ exactly once. For example, the following arrays are permutations: $$$[3, 1, 2]$$$, $$$[1]$$$, $$$[1, 2, 3, 4, 5]$$$ and $$$[4, 3, 1, 2]$$$. The following arrays are not permutations: $$$[2]$$$, $$$[1, 1]$$$, $$$[2, 3, 4]$$$.Polycarp invented a really cool permutation $$$p_1, p_2, \dots, p_n$$$ of length $$$n$$$. It is very disappointing, but he forgot this permutation. He only remembers the array $$$q_1, q_2, \dots, q_{n-1}$$$ of length $$$n-1$$$, where $$$q_i=p_{i+1}-p_i$$$.Given $$$n$$$ and $$$q=q_1, q_2, \dots, q_{n-1}$$$, help Polycarp restore the invented permutation.

The first line contains the integer $$$n$$$ ($$$2 \le n \le 2\cdot10^5$$$) — the length of the permutation to restore. The second line contains $$$n-1$$$ integers $$$q_1, q_2, \dots, q_{n-1}$$$ ($$$-n < q_i < n$$$).

Print the integer -1 if there is no such permutation of length $$$n$$$ which corresponds to the given array $$$q$$$. Otherwise, if it exists, print $$$p_1, p_2, \dots, p_n$$$. Print any such permutation if there are many of them.

object _1141C extends CodeForcesApp {
  import annotation._, collection.{mutable, Searching}, util._, control._, math.Ordering.Implicits._, Searching._, Math._

  override def apply(io: IO): io.type = {
    val n = io.read[Int]
    val diffs = io.read[Seq, Int](n - 1)

    val zero = diffs.scanLeft(0)(_ + _)
    val toAdd = 1 - zero.min
    val added = zero.map(_ + toAdd)
    val ans = if ((1 to n).toSet == added.toSet) {
      added
    } else {
      Seq(-1)
    }
    io.writeAll(ans)
  }
}
/****************************[Ignore Template Below]****************************************/
trait CodeForcesApp {
  lazy val isOnlineJudge: Boolean = sys.props.get("ONLINE_JUDGE").exists(_.toBoolean)
  def debug(x: => Any): Unit = if (!isOnlineJudge) println(x)
  def apply(io: IO): io.type
  def main(args: Array[String]): Unit = this(new IO(System.in, System.out)).close()
}
/***********************************[Scala I/O]*********************************************/
import java.io._, java.util.StringTokenizer
import scala.collection.generic.CanBuild

class IO(in: BufferedReader, out: BufferedWriter) extends Iterator[String] with AutoCloseable with Flushable {
  def this(reader: Reader, writer: Writer) = this(new BufferedReader(reader), new BufferedWriter(writer))
  def this(in: InputStream, out: OutputStream) = this(new InputStreamReader(in), new OutputStreamWriter(out))

  private[this] val tokenizers = Iterator.continually(in.readLine()).takeWhile(_ != null).map(new StringTokenizer(_)).buffered
  val printer = new PrintWriter(out, true)

  @inline private[this] def tokenizer() = {
    while(tokenizers.nonEmpty && !tokenizers.head.hasMoreTokens) tokenizers.next()
    tokenizers.headOption
  }

  def read[A](implicit read: IO.Read[A]): A = read.apply(this)
  def read[C[_], A: IO.Read](n: Int)(implicit b: CanBuild[A, C[A]]): C[A] = Iterator.fill(n)(read).to[C]

  def readTill(delim: String): String = tokenizer().get.nextToken(delim)
  def readTillEndOfLine(): String = readTill("\n\r")

  def readAll[A: IO.Read]: (Int, Vector[A]) = {
    val data = read[Vector[A]]
    (data.length, data)
  }

  override def next() = tokenizer().get.nextToken()
  override def hasNext = tokenizer().nonEmpty

  def write(obj: Any): this.type = {
    printer.print(obj)
    this
  }
  def writeLine(): this.type = {
    printer.println()
    this
  }
  def writeLine(obj: Any): this.type = {
    printer.println(obj)
    this
  }
  def writeLines(obj: Traversable[Any]): this.type = writeAll(obj, separator = "\n")
  def writeAll(obj: Traversable[Any], separator: String = " "): this.type = write(obj mkString separator)
  def query[A: IO.Read](question: Any): A = writeLine(question).read

  override def flush() = printer.flush()
  def close() = {
    flush()
    in.close()
    printer.close()
  }
}
object IO {
  class Read[A](val apply: IO => A) {
    def map[B](f: A => B): Read[B] = new Read(apply andThen f)
  }

  object Read {
    implicit def collection[C[_], A: Read](implicit b: CanBuild[A, C[A]]) : Read[C[A]]         = new Read(r => r.read[C, A](r.read[Int]))
    implicit val string                                                   : Read[String]       = new Read(_.next())
    implicit val int                                                      : Read[Int]          = string.map(_.toInt)
    implicit val long                                                     : Read[Long]         = string.map(_.toLong)
    implicit val bigInt                                                   : Read[BigInt]       = string.map(BigInt(_))
    implicit val double                                                   : Read[Double]       = string.map(_.toDouble)
    implicit val bigDecimal                                               : Read[BigDecimal]   = string.map(BigDecimal(_))
    implicit def tuple2[A: Read, B: Read]                                 : Read[(A, B)]       = new Read(r => (r.read[A], r.read[B]))
    implicit def tuple3[A: Read, B: Read, C: Read]                        : Read[(A, B, C)]    = new Read(r => (r.read[A], r.read[B], r.read[C]))
    implicit def tuple4[A: Read, B: Read, C: Read, D: Read]               : Read[(A, B, C, D)] = new Read(r => (r.read[A], r.read[B], r.read[C], r.read[D]))
    implicit val boolean                                                  : Read[Boolean]      = string map {s =>
      s.toLowerCase match {
        case "yes" | "true" | "1" => true
        case "no" | "false" | "0" => false
        case _ => s.toBoolean
      }
    }
  }
}


You are given an integer array $$$a_1, a_2, \ldots, a_n$$$.The array $$$b$$$ is called to be a subsequence of $$$a$$$ if it is possible to remove some elements from $$$a$$$ to get $$$b$$$.Array $$$b_1, b_2, \ldots, b_k$$$ is called to be good if it is not empty and for every $$$i$$$ ($$$1 \le i \le k$$$) $$$b_i$$$ is divisible by $$$i$$$.Find the number of good subsequences in $$$a$$$ modulo $$$10^9 + 7$$$. Two subsequences are considered different if index sets of numbers included in them are different. That is, the values of the elements do not matter in the comparison of subsequences. In particular, the array $$$a$$$ has exactly $$$2^n - 1$$$ different subsequences (excluding an empty subsequence).

The first line contains an integer $$$n$$$ ($$$1 \le n \le 100\,000$$$) — the length of the array $$$a$$$. The next line contains integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^6$$$).

Print exactly one integer — the number of good subsequences taken modulo $$$10^9 + 7$$$.

object Main {
  def main(args: Array[String]): Unit = {
    val s = new Main()
    s.solve()
    s.out.flush()
  }
}

class Main {
  import java.io._
  import java.util.StringTokenizer

  import scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

  val MOD = 1000000007
  val out = new PrintWriter(System.out)

  def solve(): Unit = {
    val N = ni()
    val A = na(N)
    val dp = Array.ofDim[Long](N + 1)
    dp(0) = 1

    rep(N) { i =>
      val ds = divisors(A(i), i + 1)
      rep_r(ds.length){ i =>
        val d = ds(i)
        dp(d) += dp(d - 1)
        if (dp(d) >= MOD) dp(d) -= MOD
      }
    }

    var ans = 0L
    rep(N, 1) { i =>
      ans += dp(i)
      if (ans >= MOD) ans -= MOD
    }
    out.println(ans)
  }

  def divisors(x: Int, mx: Int): Array[Int] = {
    val pre = ArrayBuffer[Int]()
    val post = ArrayBuffer[Int]()
    rep(math.sqrt(x).toInt, 1) { d =>
      if (x % d == 0) {
        if (d <= mx) pre += d
        if (d * d != x && x / d <= mx) post += x / d
      }
    }

    val res = Array.ofDim[Int](pre.length + post.length)
    rep(pre.length)(i => res(i) = pre(i))
    val preLen = pre.length
    rep_r(post.length)(i => res(i + preLen) = post(i))
    res
  }

  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next().toInt
    def nextLong(): Long = next().toLong
    def nextChar(): Char = next().charAt(0)
  }
  val sc = new InputReader(System.in)
  def ni(): Int = sc.nextInt()
  def nl(): Long = sc.nextLong()
  def nc(): Char = sc.nextChar()
  def ns(): String = sc.next()
  def ns(n: Int): Array[Char] = ns().toCharArray
  def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
  def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
    val A1, A2 = Array.ofDim[Int](n)
    rep(n) { i =>
      A1(i) = ni() + offset
      A2(i) = ni() + offset
    }
    (A1, A2)
  }
  def nm(n: Int, m: Int): Array[Array[Int]] = {
    val A = Array.ofDim[Int](n, m)
    rep(n) { i =>
      rep(m) { j =>
        A(i)(j) = ni()
      }
    }
    A
  }
  def nal(n: Int): Array[Long] = map(n)(_ => nl())
  def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  def rep(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def rep_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }

  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    rep(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    rep(as.length)(i => s += as(i))
    s
  }

  def cumSum(as: Array[Int]) = {
    val cum = Array.ofDim[Int](as.length + 1)
    rep(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

object Main {
  def main(args: Array[String]): Unit = {
    val s = new Main()
    s.solve()
    s.out.flush()
  }
}

class Main {
  import java.io._
  import java.util.StringTokenizer

  import scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

  val MOD = 1000000007
  val out = new PrintWriter(System.out)

  def solve(): Unit = {
    val N = ni()
    val A = na(N)

    val MAX = 1e6.toInt
    val divisors = Array.fill[ArrayBuffer[Int]](1e6.toInt + 1)(ArrayBuffer())
    val hasA = Array.ofDim[Boolean](MAX + 1)
    rep(N) { i =>
      hasA(A(i)) = true
    }

    rep_r(MAX, 1) { d =>
      var j = d
      while(j <= MAX) {
        if (hasA(j)) {
          divisors(j) += d
        }
        j += d
      }
    }

    val table = Array.ofDim[Long](N + 1)
    table(0) = 1
    rep(N) { i =>
      val ds = divisors(A(i))
      rep(ds.length) { j =>
        val d = ds(j)
        if (d <= i + 1) {
          table(d) += table(d - 1)
          if (table(d) >= MOD) table(d) -= MOD
        }
      }
    }
    var ans = 0L
    rep(N) { i =>
      ans += table(i + 1)
      if (ans >= MOD) ans -= MOD
    }

    out.println(ans)
  }

  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next().toInt
    def nextLong(): Long = next().toLong
    def nextChar(): Char = next().charAt(0)
  }
  val sc = new InputReader(System.in)
  def ni(): Int = sc.nextInt()
  def nl(): Long = sc.nextLong()
  def nc(): Char = sc.nextChar()
  def ns(): String = sc.next()
  def ns(n: Int): Array[Char] = ns().toCharArray
  def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
  def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
    val A1, A2 = Array.ofDim[Int](n)
    rep(n) { i =>
      A1(i) = ni() + offset
      A2(i) = ni() + offset
    }
    (A1, A2)
  }
  def nm(n: Int, m: Int): Array[Array[Int]] = {
    val A = Array.ofDim[Int](n, m)
    rep(n) { i =>
      rep(m) { j =>
        A(i)(j) = ni()
      }
    }
    A
  }
  def nal(n: Int): Array[Long] = map(n)(_ => nl())
  def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  def rep(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def rep_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }

  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    rep(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    rep(as.length)(i => s += as(i))
    s
  }

  def cumSum(as: Array[Int]) = {
    val cum = Array.ofDim[Int](as.length + 1)
    rep(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

object Main {
  def main(args: Array[String]): Unit = {
    val s = new Main()
    s.solve()
    s.out.flush()
  }
}

class Main {
  import java.io._
  import java.util.StringTokenizer

  import scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

  val MOD = 1000000007
  val out = new PrintWriter(System.out)

  def solve(): Unit = {
    val N = ni()
    val A = na(N)
    val table = Array.ofDim[Long](N + 1)
    table(0) = 1
    rep(N) { i =>
      val ds = dividers(A(i))
      Sorting.quickSort(ds)
      rep_r(ds.length) { j =>
        val d = ds(j)
        if (d <= N) {
          table(d) += table(d - 1)
          if (table(d) >= MOD) table(d) -= MOD
        }
      }
    }
    var ans = 0L
    rep(N) { i =>
      ans += table(i + 1)
    }

    out.println(ans)
  }

  def dividers(x: Int): Array[Int] = {
    if (x == 1) Array(1)
    else {
      val fs_map = factorize(x)
      val fs = fs_map.toArray
      val len = fs.length
      val digits = Array.ofDim[Int](len)
      val res = Array.ofDim[Int](fs_map.map(_._2 + 1).product)
      var p = 0

      def next(): Boolean = {
        var k = 0
        digits(0) += 1
        while (k < len - 1) {
          if (digits(k) > fs(k)._2) {
            digits(k) = 0
            digits(k + 1) += 1
            k += 1
          } else {
            return true
          }
        }
        digits(len - 1) <= fs(len - 1)._2
      }

      def mkVal: Int = {
        var res = 1
        rep(len) { i =>
          rep(digits(i)) { _ =>
            res *= fs(i)._1
          }
        }
        res
      }

      res(0) = 1
      p += 1
      while (next()) {
        res(p) = mkVal
        p += 1
      }

      res
    }
  }

  def factorize(n: Int): mutable.Map[Int, Int] = {
    import scala.collection.mutable
    val res = mutable.Map[Int, Int]() withDefaultValue 0

    def minFactor(n0: Int, rt: Int, i: Int): Int = {
      if (i > rt) n0 // √n まで見つからなかったら n0は素数か1
      else if (n0 % i == 0) i
      else minFactor(n0, rt, i + 1)
    }

    def step(n0: Int): Unit = {
      minFactor(n0, math.sqrt(n0).toInt, 2) match {
        case 1 =>
        case f =>
          res(f) = res(f) + 1
          step(n0 / f)
      }
    }

    step(n)
    res
  }

  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next().toInt
    def nextLong(): Long = next().toLong
    def nextChar(): Char = next().charAt(0)
  }
  val sc = new InputReader(System.in)
  def ni(): Int = sc.nextInt()
  def nl(): Long = sc.nextLong()
  def nc(): Char = sc.nextChar()
  def ns(): String = sc.next()
  def ns(n: Int): Array[Char] = ns().toCharArray
  def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
  def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
    val A1, A2 = Array.ofDim[Int](n)
    rep(n) { i =>
      A1(i) = ni() + offset
      A2(i) = ni() + offset
    }
    (A1, A2)
  }
  def nm(n: Int, m: Int): Array[Array[Int]] = {
    val A = Array.ofDim[Int](n, m)
    rep(n) { i =>
      rep(m) { j =>
        A(i)(j) = ni()
      }
    }
    A
  }
  def nal(n: Int): Array[Long] = map(n)(_ => nl())
  def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  def rep(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def rep_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }

  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    rep(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    rep(as.length)(i => s += as(i))
    s
  }

  def cumSum(as: Array[Int]) = {
    val cum = Array.ofDim[Int](as.length + 1)
    rep(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}


There is a house with $$$n$$$ flats situated on the main street of Berlatov. Vova is watching this house every night. The house can be represented as an array of $$$n$$$ integer numbers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i = 1$$$ if in the $$$i$$$-th flat the light is on and $$$a_i = 0$$$ otherwise.Vova thinks that people in the $$$i$$$-th flats are disturbed and cannot sleep if and only if $$$1 < i < n$$$ and $$$a_{i - 1} = a_{i + 1} = 1$$$ and $$$a_i = 0$$$.Vova is concerned by the following question: what is the minimum number $$$k$$$ such that if people from exactly $$$k$$$ pairwise distinct flats will turn off the lights then nobody will be disturbed? Your task is to find this number $$$k$$$.

The first line of the input contains one integer $$$n$$$ ($$$3 \le n \le 100$$$) — the number of flats in the house. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$a_i \in \{0, 1\}$$$), where $$$a_i$$$ is the state of light in the $$$i$$$-th flat.

Print only one integer — the minimum number $$$k$$$ such that if people from exactly $$$k$$$ pairwise distinct flats will turn off the light then nobody will be disturbed.

NoteIn the first example people from flats $$$2$$$ and $$$7$$$ or $$$4$$$ and $$$7$$$ can turn off the light and nobody will be disturbed. It can be shown that there is no better answer in this example.There are no disturbed people in second and third examples.

object Main {
  def main(args: Array[String]): Unit = {
    val s = new Main()
    s.solve()
    s.out.flush()
  }
}

class Main {
  import java.io._
  import java.util.StringTokenizer

  import scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

  val MOD = 1000000007
  val out = new PrintWriter(System.out)

  def solve(): Unit = {
    val N = ni()
    val A = na(N)
    val flg = Array.ofDim[Boolean](N)
    var ans = 0
    rep(N - 2, 1) { i =>
      if (A(i - 1) == 1 && A(i + 1) == 1 && A(i) == 0) {
        flg(i) = true
      }
    }

    rep(N - 2, 1) { i =>
      if (flg(i)) {
        flg(i) = false
        if (i < N - 2) flg(i + 2) = false
        ans += 1
      }
    }

    out.println(ans)
  }

  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next().toInt
    def nextLong(): Long = next().toLong
    def nextChar(): Char = next().charAt(0)
  }
  val sc = new InputReader(System.in)
  def ni(): Int = sc.nextInt()
  def nl(): Long = sc.nextLong()
  def nc(): Char = sc.nextChar()
  def ns(): String = sc.next()
  def ns(n: Int): Array[Char] = ns().toCharArray
  def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
  def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
    val A1, A2 = Array.ofDim[Int](n)
    rep(n) { i =>
      A1(i) = ni() + offset
      A2(i) = ni() + offset
    }
    (A1, A2)
  }
  def nm(n: Int, m: Int): Array[Array[Int]] = {
    val A = Array.ofDim[Int](n, m)
    rep(n) { i =>
      rep(m) { j =>
        A(i)(j) = ni()
      }
    }
    A
  }
  def nal(n: Int): Array[Long] = map(n)(_ => nl())
  def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  def rep(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def rep_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }

  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    rep(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    rep(as.length)(i => s += as(i))
    s
  }

  def cumSum(as: Array[Int]) = {
    val cum = Array.ofDim[Int](as.length + 1)
    rep(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

import scala.io.StdIn

object B extends App {
	val n = StdIn.readInt
	val arr = StdIn.readLine.split(" ").map(_.toInt)

	def f(p: Int, cnt: Int): Int = {
		if(p >= n)
			cnt

		else {
			if(p + 2 < n && arr(p) == 1 && arr(p + 1) == 0 && arr(p + 2) == 1)
				f(p + 3, cnt + 1)

			else f(p + 1, cnt)
		}
	}

	println(f(0, 0))
}

object CF_521_3_B {
  def solve(n: Int, as: Seq[Int]): Int = {
    def turnOff(as: Seq[Int], count: Int, out: Seq[Int]): (Seq[Int], Int) = as match {
      case 1 +: 0 +: 1 +: _  => turnOff(as.updated(2,0).tail, count + 1, out :+ 1)
      case a +: _ +: _ +: _  => turnOff(as.tail, count, out :+ a)
      case Seq(a,b) => (out :+ a :+ b, count)
    }
    val result = turnOff(as, 0, Seq.empty)
    result._2
  }

  // `solution` is defined by the input format - and can be called both from main and test script without repeating
  def solution(i: Input) = solve(i.int, {i.nextLine; i.intSeq()})

//     ~~~ Boilerplate that doesn't change ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def main(args: Array[String]) = {
    val out = new java.io.PrintWriter(System.out)
    val sol = solution(new Input(System.in))
    out.println(sol)
    out.close()
  }

//     ~~~ Utility methods ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  import java.util.Scanner
  class Input(sc: Scanner) {
    def this(i: java.io.InputStream) = this(new Scanner(i))
    def this(s: String) = this(new Scanner(s.stripMargin))
    def int = sc.nextInt()
    def long = sc.nextLong()
    def double = sc.nextDouble()
    def nextLine = sc.nextLine()
    // If there are prior values, remember to call nextLine to advance past the newline character before collecting the line:
    def collect[T](f: String => T) = {sc.nextLine().split(" ").toVector.map(f)}
    def intSeq() = collect(_.toInt)
    def doubleSeq() = collect(_.toDouble)
  }

  def ceil(a: Int, b: Int) = (a - 1) / b + 1
  def ceil(a: Long, b: Long) = (a - 1) / b + 1

  import language.implicitConversions
  implicit def stringToInput(s: String): Input = new Input(s)
}

import scala.io.StdIn
import scala.collection.mutable.Buffer

object Main {
  def main(args: Array[String]): Unit = {
    val n = StdIn.readLine()
    val flats: Array[Int] = StdIn.readLine().split(" ").map(_.toInt)
    val pr = getPriorities(flats)
    val after2Deleting = handlePriorities(pr.toBuffer, 2)
    val after1Deleting = handlePriorities(after2Deleting._2, 1)
    println(after2Deleting._1 + after1Deleting._1)
   }

  def getPriorities(flats: Array[Int]): IndexedSeq[Int]  = {
    for(i <- flats.indices) yield getPriority(flats, i)
  }

  def getPriority(flats: Array[Int], i: Int): Int = {
    flats(i) match {
      case 0 => 0
      case 1 =>
        val tail = flats.length - 1
        val preTail = flats.length - 2
        i match {
          case 0 => {
            if(flats(1) == 0 && flats(2) == 1) 1
            else 0
          }
          case 1 => {
            if(flats.length != 3 && flats(2) == 0 && flats(3) == 1) 1
            else 0
          }
          case `tail` => {
            if(flats(flats.length - 2) == 0 && flats(flats.length - 3) == 1) 1
            else 0
          }
          case `preTail` => {
            if(flats(flats.length - 3) == 0 && flats(flats.length - 4) == 1) 1
            else 0
          }
          case other => {
            val right = (flats(other + 1), flats(other + 2)) match {
              case (0, 1) => 1
              case _ => 0
            }
            val left = (flats(other - 1), flats(other - 2)) match {
              case (0, 1) => 1
              case _ => 0
            }
            right + left
          }
        }
    }
  }

  def handlePriorities(priorities: Buffer[Int], num: Int) = {
    var counter: Int = 0
    for(i <- priorities.indices) {
     priorities(i) match {
       case `num` => {
         priorities(i) = 0
         if(i - 2 >= 0) priorities(i - 2) -= 1
         if(i + 2 < priorities.length)priorities(i + 2) -= 1
         counter += 1
       }
       case _ =>
     }
    }
    (counter, priorities)
  }
}

object A extends App{
    import java.io.{PrintWriter}
    import java.util.{Locale, Scanner}

    val in = new Scanner(System.in)
    val out = new PrintWriter(System.out)

    def nextInt = in.nextInt

    def solve = {
        val n = nextInt
        var ans = 0
        var stats = (for(i <- 1 to n) yield nextInt).toList
        stats = (0::stats):+0
        var input = stats.toBuffer

        for (i <- 1 to n) {
            if(input(i) == 0) {
                if(input(i - 1) == 1 && input(i + 1) == 1) {
                    input(i + 1) = 0
                    ans += 1
                }
            }
        }
        println(ans)
    }

    solve
}


Vasya has two arrays $$$A$$$ and $$$B$$$ of lengths $$$n$$$ and $$$m$$$, respectively.He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $$$[1, 10, 100, 1000, 10000]$$$ Vasya can obtain array $$$[1, 1110, 10000]$$$, and from array $$$[1, 2, 3]$$$ Vasya can obtain array $$$[6]$$$.Two arrays $$$A$$$ and $$$B$$$ are considered equal if and only if they have the same length and for each valid $$$i$$$ $$$A_i = B_i$$$.Vasya wants to perform some of these operations on array $$$A$$$, some on array $$$B$$$, in such a way that arrays $$$A$$$ and $$$B$$$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $$$A$$$ and $$$B$$$ equal.

The first line contains a single integer $$$n~(1 \le n \le 3 \cdot 10^5)$$$ — the length of the first array. The second line contains $$$n$$$ integers $$$a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$$$ — elements of the array $$$A$$$. The third line contains a single integer $$$m~(1 \le m \le 3 \cdot 10^5)$$$ — the length of the second array. The fourth line contains $$$m$$$ integers $$$b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$$$ - elements of the array $$$B$$$.

Print a single integer — the maximum length of the resulting arrays after some operations were performed on arrays $$$A$$$ and $$$B$$$ in such a way that they became equal. If there is no way to make array equal, print "-1".

object Main {
  def main(args: Array[String]): Unit = {
    val s = new Main()
    s.solve()
    s.out.flush()
  }
}

class Main {
  import java.io._
  import java.util.StringTokenizer

  import scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

  val MOD = 1000000007
  val out = new PrintWriter(System.out)

  def solve(): Unit = {
    val N = ni()
    val A = map(N)(_ => nl())
    val M = ni()
    val B = map(M)(_ => nl())

    if (A.sum != B.sum) out.println(-1)
    else {
      val cumA = Array.ofDim[Long](N)
      cumA(0) = A(0)
      rep(N - 1) { i =>
        cumA(i + 1) = cumA(i) + A(i + 1)
      }
      val cumB = Array.ofDim[Long](M)
      cumB(0) = B(0)
      rep(M - 1) { i =>
        cumB(i + 1) = cumB(i) + B(i + 1)
      }

      var ans = 0
      var a, b = 0
      while (a < N && b < M) {
        if (cumA(a) == cumB(b)) {
          ans += 1
          a += 1
          b += 1
        } else if (cumA(a) > cumB(b)) {
          b += 1
        } else {
          a += 1
        }
      }

      out.println(if (ans == 0) -1 else ans)
    }
  }

  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next().toInt
    def nextLong(): Long = next().toLong
    def nextChar(): Char = next().charAt(0)
  }
  val sc = new InputReader(System.in)
  def ni(): Int = sc.nextInt()
  def nl(): Long = sc.nextLong()
  def nc(): Char = sc.nextChar()
  def ns(): String = sc.next()
  def ns(n: Int): Array[Char] = ns().toCharArray
  def na(n: Int): Array[Int] = map(n)(_ => ni())
  def nm(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  def rep(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def rep_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }

  def map[@specialized A: ClassTag](n: Int)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    rep(n)(i => res(i) = f(i))
    res
  }
}

object Main {
import java.io._
import java.util.Scanner

import collection.mutable.HashMap
import scala.collection.{SortedSet, mutable}
import scala.util.Random
  protected val EPS = 1e-11

  private def default = (
    new StreamTokenizer(new InputStreamReader(System.in)),
    new Scanner(System.in),
    new PrintWriter(System.out)
  )

  val (in, ins, out) = {
    try {
      if (sys.props.contains("xDx")) {
        (
          new StreamTokenizer(new FileReader("input.txt")),
          new Scanner(new FileReader("input.txt")),
          new PrintWriter(new FileWriter("output.txt"))
        )
      } else {
        default
      }
    } catch {
      case _: Throwable => default
    }
  }

  private def nextInt = {
    in.nextToken
    in.nval.toInt
  }

  private def nextLong = {
    in.nextToken
    in.nval.toLong
  }

  private def nextDouble = {
    in.nextToken
    in.nval
  }

  private def nextString = {
    in.nextToken
    in.sval
  }

  private def nextChar = {
    in.nextToken
    in.ttype.toChar
  }

  def main(args: Array[String]): Unit = {
    try {
      new Run().run()
    } catch {
      case e: Throwable =>
        throw new RuntimeException(e)
    } finally out.close()
  }

  class Run {
    import collection.breakOut
    def run(): Unit = {
        val n = nextInt
        val a = readArr(n)
        val m = nextInt
        val b = readArr(m)
        if(a(n - 1) != b(m - 1)) {
            println(-1)
        } else {
            var res = 0
            var ai = 0
            var bi = 0
            while(ai < n && bi < m) {
                if(a(ai) == b(bi)) {
                    res += 1
                    ai += 1
                    bi += 1
                } else if(a(ai) < b(bi)) {
                    ai += 1
                } else {
                    bi += 1
                }
            }
            println(res)
        }
    }

    def readArr(n: Int): Array[Long] = {
        val a = Array.ofDim[Long](n)
        var prev = 0L
        for(i <- 0 until n) {
            a(i) = prev + nextInt
            prev = a(i)
        }
        a
    }

  }

}

object Main {
  def main(args: Array[String]): Unit = {
    val s = new Main()
    s.solve()
    s.out.flush()
  }
}

class Main {
  import java.io._
  import java.util.StringTokenizer

  import scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

  val MOD = 1000000007
  val out = new PrintWriter(System.out)

  def solve(): Unit = {
    val N = ni()
    val A = na(N)
    val M = ni()
    val B = na(M)

    val cumA = Array.ofDim[Long](N)
    cumA(0) = A(0)
    rep(N - 1) { i =>
      cumA(i + 1) = cumA(i) + A(i + 1)
    }
    val cumB = Array.ofDim[Long](M)
    cumB(0) = B(0)
    rep(M - 1) { i =>
      cumB(i + 1) = cumB(i) + B(i + 1)
    }

    var ans = 0
    var a, b = 0
    while(a < N && b < M) {
      if (cumA(a) == cumB(b)) {
        ans += 1
        a += 1
        b += 1
      } else if (cumA(a) > cumB(b)) {
        b += 1
      } else {
        a += 1
      }
    }

    out.println(if (ans == 0) - 1 else ans)
  }

  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next().toInt
    def nextLong(): Long = next().toLong
    def nextChar(): Char = next().charAt(0)
  }
  val sc = new InputReader(System.in)
  def ni(): Int = sc.nextInt()
  def nl(): Long = sc.nextLong()
  def nc(): Char = sc.nextChar()
  def ns(): String = sc.next()
  def ns(n: Int): Array[Char] = ns().toCharArray
  def na(n: Int): Array[Int] = map(n)(_ => ni())
  def nm(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  def rep(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def rep_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }

  def map[@specialized A: ClassTag](n: Int)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    rep(n)(i => res(i) = f(i))
    res
  }
}

object Main {
  def main(args: Array[String]): Unit = {
    val s = new Main()
    s.solve()
    s.out.flush()
  }
}

class Main {
  import java.io._
  import java.util.StringTokenizer

  import scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

  val MOD = 1000000007
  val out = new PrintWriter(System.out)

  def solve(): Unit = {
    val N = ni()
    val A = map(N)(_ => ni())
    val M = ni()
    val B = map(M)(_ => ni())

    if (A.sum != B.sum) out.println(-1)
    else {
      val cumA = Array.ofDim[Long](N)
      cumA(0) = A(0)
      rep(N - 1) { i =>
        cumA(i + 1) = cumA(i) + A(i + 1)
      }
      val cumB = Array.ofDim[Long](M)
      cumB(0) = B(0)
      rep(M - 1) { i =>
        cumB(i + 1) = cumB(i) + B(i + 1)
      }

      var ans = 0
      var a, b = 0
      while (a < N && b < M) {
        if (cumA(a) == cumB(b)) {
          ans += 1
          a += 1
          b += 1
        } else if (cumA(a) > cumB(b)) {
          b += 1
        } else {
          a += 1
        }
      }

      out.println(if (ans == 0) -1 else ans)
    }
  }

  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next().toInt
    def nextLong(): Long = next().toLong
    def nextChar(): Char = next().charAt(0)
  }
  val sc = new InputReader(System.in)
  def ni(): Int = sc.nextInt()
  def nl(): Long = sc.nextLong()
  def nc(): Char = sc.nextChar()
  def ns(): String = sc.next()
  def ns(n: Int): Array[Char] = ns().toCharArray
  def na(n: Int): Array[Int] = map(n)(_ => ni())
  def nm(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  def rep(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def rep_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }

  def map[@specialized A: ClassTag](n: Int)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    rep(n)(i => res(i) = f(i))
    res
  }
}

object Main {
  def main(args: Array[String]): Unit = {
    val s = new Main()
    s.solve()
    s.out.flush()
  }
}

class Main {
  import java.io._
  import java.util.StringTokenizer

  import scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

  val MOD = 1000000007
  val out = new PrintWriter(System.out)

  def solve(): Unit = {
    val N = ni()
    val A = na(N)
    val M = ni()
    val B = na(M)

    if (A.sum != B.sum) out.println(-1)
    else {
      val cumA = Array.ofDim[Long](N)
      cumA(0) = A(0)
      rep(N - 1) { i =>
        cumA(i + 1) = cumA(i) + A(i + 1)
      }
      val cumB = Array.ofDim[Long](M)
      cumB(0) = B(0)
      rep(M - 1) { i =>
        cumB(i + 1) = cumB(i) + B(i + 1)
      }

      var ans = 0
      var a, b = 0
      while (a < N && b < M) {
        if (cumA(a) == cumB(b)) {
          ans += 1
          a += 1
          b += 1
        } else if (cumA(a) > cumB(b)) {
          b += 1
        } else {
          a += 1
        }
      }

      out.println(if (ans == 0) -1 else ans)
    }
  }

  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next().toInt
    def nextLong(): Long = next().toLong
    def nextChar(): Char = next().charAt(0)
  }
  val sc = new InputReader(System.in)
  def ni(): Int = sc.nextInt()
  def nl(): Long = sc.nextLong()
  def nc(): Char = sc.nextChar()
  def ns(): String = sc.next()
  def ns(n: Int): Array[Char] = ns().toCharArray
  def na(n: Int): Array[Int] = map(n)(_ => ni())
  def nm(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  def rep(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def rep_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }

  def map[@specialized A: ClassTag](n: Int)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    rep(n)(i => res(i) = f(i))
    res
  }
}


One day Vasya painted a Cartesian coordinate system on a piece of paper and marked some set of points (x1, y1), (x2, y2), ..., (xn, yn). Let's define neighbors for some fixed point from the given set (x, y):   point (x', y') is (x, y)'s right neighbor, if x' > x and y' = y  point (x', y') is (x, y)'s left neighbor, if x' < x and y' = y  point (x', y') is (x, y)'s lower neighbor, if x' = x and y' < y  point (x', y') is (x, y)'s upper neighbor, if x' = x and y' > y We'll consider point (x, y) from the given set supercentral, if it has at least one upper, at least one lower, at least one left and at least one right neighbor among this set's points.Vasya marked quite many points on the paper. Analyzing the picture manually is rather a challenge, so Vasya asked you to help him. Your task is to find the number of supercentral points in the given set.

The first input line contains the only integer n (1 ≤ n ≤ 200) — the number of points in the given set. Next n lines contain the coordinates of the points written as "x y" (without the quotes) (|x|, |y| ≤ 1000), all coordinates are integers. The numbers in the line are separated by exactly one space. It is guaranteed that all points are different.

Print the only number — the number of supercentral points of the given set.

NoteIn the first sample the supercentral points are only points (1, 1) and (1, 2).In the second sample there is one supercental point — point (0, 0).

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val n = in.next().toInt
  val data = (1 to n).map{_ =>
    val Array(a, b) = in.next().split(" ").map(_.toInt)
    (a, b)
  }
  var count = 0
  println((0 until n).count { i =>
    val (a, b) = data(i)
    var up = false
    var down = false
    var left = false
    var right = false
    (0 until n).foreach { j =>
      val (a1, b1) = data(j)
      if (a1 == a && b1 > b)
        up = true
      else if (a1 == a && b1 < b)
        down = true
      else if (b1 == b && a1 < a)
        right = true
      else if (b1 == b && a1 > a)
        left = true
    }
    up && left && down && right
  })

}

object A165 {

  import IO._
  import collection.{mutable => cu}

  def main(args: Array[String]): Unit = {
    val Array(n) = readInts(1)
    val in = Array.fill(n)(readInts(2)).map(arr => (arr(0), arr(1)))
    var res = 0
    for(i <- 0 until n) {
      var (right, left, upper, lower) = (false, false, false, false)
      for(j <- 0 until n if i != j) {
        if(in(i)._2 == in(j)._2) {
          if(in(i)._1 > in(j)._1)
            right = true
          else if(in(i)._1 < in(j)._1)
            left = true
        } else if(in(i)._1 == in(j)._1) {
          if(in(i)._2 > in(j)._2)
            upper = true
          else if(in(i)._2 < in(j)._2)
            lower = true
        }
      }
      if(right && left && upper && lower)
        res += 1
    }
    println(res)
  }

  object IO {
    @inline def read: String = scala.io.StdIn.readLine

    @inline def tokenizeLine = new java.util.StringTokenizer(read)

    def readInts(n: Int): Array[Int] = {
      val tl = tokenizeLine;
      Array.fill(n)(tl.nextToken.toInt)
    }

    def readLongs(n: Int): Array[Long] = {
      val tl = tokenizeLine;
      Array.fill(n)(tl.nextToken.toLong)
    }

    def readBigs(n: Int): Array[BigInt] = {
      val tl = tokenizeLine;
      Array.fill(n)(BigInt(tl.nextToken))
    }
  }

}

import java.util.Scanner
import java.io.PrintWriter
import scala.annotation.tailrec
import scala.collection.mutable
import scala.util.Random

object P165A extends App {
  val sc = new Scanner(System.in)
  val out = new PrintWriter(System.out)

  val N = sc.nextInt
  val a = Array.fill(N, 2)(sc.nextInt)

  def isSupercentral(p: Array[Int]): Boolean = {
    @tailrec
    def loop(right: Int, left: Int, lower: Int, upper: Int, i: Int): Boolean =
      if (i == N) right > 0 && left > 0 && lower > 0 && upper > 0
      else {
        val q = a(i)
        if (p(0) == q(0) && p(1) < q(1)) loop(right, left, lower, upper + 1, i + 1)
        else if (p(0) == q(0) && p(1) > q(1)) loop(right, left, lower + 1, upper, i + 1)
        else if (p(1) == q(1) && p(0) < q(0)) loop(right, left + 1, lower, upper, i + 1)
        else if (p(1) == q(1) && p(0) > q(0)) loop(right + 1, left, lower, upper, i + 1)
        else loop(right, left, lower, upper, i + 1)
      }
    loop(0, 0, 0, 0, 0)
  }

  val answer = (0 until N).filter((x: Int) => isSupercentral(a(x))).size

  out.println(answer)
  out.close
}

import annotation.tailrec
import java.io._

object Easy {
  val reader = new BufferedReader(new InputStreamReader(System.in))
  def readInts = reader.readLine().split(" ").map(_.toInt)
  def readInt = reader.readLine().toInt

  val n = readInt
  val points = for(_ <- 1 to n) yield readInts
  def ans = points.count {
    case Array(x, y) => (for(i <- 0 until 4) yield {
      val dx = (i & 1) * i - 2 * (i & 1)
      val dy = (1 - i) * (1 - (i & 1))
      points.count {
        case Array(x1, y1) => (x - x1).signum == dx && (y - y1).signum == dy
      }
    }).reduce(_ * _) != 0
  }

  def main(args: Array[String]) {
    println(ans)
  }
}

object Main {
  case class Point(x: Int, y: Int)
  class System {
    val xmap = scala.collection.mutable.Map[Int, List[Point]]()
    val ymap = scala.collection.mutable.Map[Int, List[Point]]()

    def read() = {
      val a = readLine().split(" ").map(_.toInt)
      Point(a(0), a(1))
    }

    def add() {
      val p = read
      xmap(p.x) = xmap.getOrElse(p.x, List[Point]()) ::: List(p)
      ymap(p.y) = ymap.getOrElse(p.y, List[Point]()) ::: List(p)
    }

    def count = {
      val x = xmap.values.map(_.sortBy(_.y))
      val xs = x.flatMap{ x0 =>
        if (x0.size >= 3) {
          x0.slice(1, x0.size - 1)
        } else Nil
      }
      val y = ymap.values.map(_.sortBy(_.x))
      val ys = y.flatMap{ y0 =>
        if (y0.size >= 3) {
          y0.slice(1, y0.size - 1)
        } else Nil
      }

      (xs.toSet & ys.toSet).size
    }
  }

  def main(args: Array[String]) {
    val n = readInt
    val s = new System()
    for (_ <- 1 to n) { s.add() }
    println(s.count)
  }
}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val n = in.next().toInt
  val data = (1 to n).map{_ =>
    val Array(a, b) = in.next().split(" ").map(_.toInt)
    (a, b)
  }
  var count = 0
  println((0 until n).count { i =>
    val (a, b) = data(i)
    var up = false
    var down = false
    var left = false
    var right = false
    (1 until n).foreach { j =>
      val (a1, b1) = data(j)
      if (a1 == a && b1 > b)
        up = true
      else if (a1 == a && b1 < b)
        down = true
      else if (b1 == b && a1 < a)
        right = true
      else if (b1 == b && a1 > a)
        left = true
    }
    up && left && down && right
  })

}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val n = in.next().toInt
  val data = (1 to n).map{_ =>
    val Array(a, b) = in.next().split(" ").map(_.toInt)
    (a, b)
  }.groupBy(_._1)
  println(data.foldLeft(0) {
    case(acc, (a, b)) if b.size < 3 => acc
    case(acc, (a, b)) => acc + b.size - 2
  })

}


So the Beautiful Regional Contest (BeRC) has come to an end! $$$n$$$ students took part in the contest. The final standings are already known: the participant in the $$$i$$$-th place solved $$$p_i$$$ problems. Since the participants are primarily sorted by the number of solved problems, then $$$p_1 \ge p_2 \ge \dots \ge p_n$$$.Help the jury distribute the gold, silver and bronze medals. Let their numbers be $$$g$$$, $$$s$$$ and $$$b$$$, respectively. Here is a list of requirements from the rules, which all must be satisfied:  for each of the three types of medals, at least one medal must be awarded (that is, $$$g>0$$$, $$$s>0$$$ and $$$b>0$$$);  the number of gold medals must be strictly less than the number of silver and the number of bronze (that is, $$$g<s$$$ and $$$g<b$$$, but there are no requirements between $$$s$$$ and $$$b$$$);  each gold medalist must solve strictly more problems than any awarded with a silver medal;  each silver medalist must solve strictly more problems than any awarded a bronze medal;  each bronze medalist must solve strictly more problems than any participant not awarded a medal;  the total number of medalists $$$g+s+b$$$ should not exceed half of all participants (for example, if $$$n=21$$$, then you can award a maximum of $$$10$$$ participants, and if $$$n=26$$$, then you can award a maximum of $$$13$$$ participants). The jury wants to reward with medals the total maximal number participants (i.e. to maximize $$$g+s+b$$$) so that all of the items listed above are fulfilled. Help the jury find such a way to award medals.

The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. The first line of a test case contains an integer $$$n$$$ ($$$1 \le n \le 4\cdot10^5$$$) — the number of BeRC participants. The second line of a test case contains integers $$$p_1, p_2, \dots, p_n$$$ ($$$0 \le p_i \le 10^6$$$), where $$$p_i$$$ is equal to the number of problems solved by the $$$i$$$-th participant from the final standings. The values $$$p_i$$$ are sorted in non-increasing order, i.e. $$$p_1 \ge p_2 \ge \dots \ge p_n$$$. The sum of $$$n$$$ over all test cases in the input does not exceed $$$4\cdot10^5$$$.

Print $$$t$$$ lines, the $$$j$$$-th line should contain the answer to the $$$j$$$-th test case. The answer consists of three non-negative integers $$$g, s, b$$$.   Print $$$g=s=b=0$$$ if there is no way to reward participants with medals so that all requirements from the statement are satisfied at the same time.  Otherwise, print three positive numbers $$$g, s, b$$$ — the possible number of gold, silver and bronze medals, respectively. The sum of $$$g+s+b$$$ should be the maximum possible. If there are several answers, print any of them.

NoteIn the first test case, it is possible to reward $$$1$$$ gold, $$$2$$$ silver and $$$3$$$ bronze medals. In this case, the participant solved $$$5$$$ tasks will be rewarded with the gold medal, participants solved $$$4$$$ tasks will be rewarded with silver medals, participants solved $$$2$$$ or $$$3$$$ tasks will be rewarded with bronze medals. Participants solved exactly $$$1$$$ task won't be rewarded. It's easy to see, that in this case, all conditions are satisfied and it is possible to reward participants in this way. It is impossible to give more than $$$6$$$ medals because the number of medals should not exceed half of the number of participants. The answer $$$1$$$, $$$3$$$, $$$2$$$ is also correct in this test case.In the second and third test cases, it is impossible to reward medals, because at least one medal of each type should be given, but the number of medals should not exceed half of the number of participants.

object Main {
  import java.io.{BufferedReader, InputStream, InputStreamReader}
  import java.util.StringTokenizer
  import scala.reflect.ClassTag

  def main(args: Array[String]): Unit = {
    val out = new java.io.PrintWriter(System.out)
    new Main(out, new InputReader(System.in)).solve()
    out.flush()
  }

  private[this] val oj = System.getenv("ATCODER_DEBUG") == null
  @inline private final def DEBUG(f: => Unit): Unit = {
    if (!oj){ f }
  }
  @inline private final def debug(as: Array[Boolean]): Unit = if (!oj){ debug(as.map(x => if(x) "1" else "0").mkString) }
  @inline def debug(as: Array[Int]): Unit = if (!oj){ debug(as.mkString(" ")) }
  @inline private final def debug(as: Array[Long]): Unit =if (!oj){ debug(as.mkString(" ")) }
  @inline private final def debugDim(m: Array[Array[Int]]): Unit = if (!oj){
    REP(m.length) { i =>
      debug(m(i))
    }
  }
  @inline private final def debugDimFlip(m: Array[Array[Long]]): Unit = if (!oj){
    REP(m(0).length) { j =>
      REP(m.length) { i =>
        System.err.print(m(i)(j))
        System.err.print(" ")
      }
      System.err.println()
    }
  }
  @inline def debug(s: => String): Unit = DEBUG {
    System.err.println(s)
  }
  @inline private final def debugL(num: => Long): Unit = DEBUG {
    System.err.println(num)
  }
  private def isDebug[A](debug: => A, online: => A): A = {
    if (oj) online else debug
  }

  class InputReader(val stream: InputStream) {
    private[this] val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private[this] var tokenizer: StringTokenizer = _

    @inline private[this] final def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = Integer.parseInt(next())
    def nextLong(): Long = java.lang.Long.parseLong(next())
    def nextChar(): Char = next().charAt(0)

    def ni(): Int = nextInt()
    def nl(): Long = nextLong()
    def nc(): Char = nextChar()
    def ns(): String = next()
    def ns(n: Int): Array[Char] = ns().toCharArray
    def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
    def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
      val A1, A2 = Array.ofDim[Int](n)
      REP(n) { i =>
        A1(i) = ni() + offset
        A2(i) = ni() + offset
      }
      (A1, A2)
    }
    def nm(n: Int, m: Int): Array[Array[Int]] = {
      val A = Array.ofDim[Int](n, m)
      REP(n) { i =>
        REP(m) { j =>
          A(i)(j) = ni()
        }
      }
      A
    }
    def nal(n: Int): Array[Long] = map(n)(_ => nl())
    def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  }

  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }
  def TO(from: Int, to: Int)(f: Int => Unit): Unit = {
    REP(to - from + 1, from)(f)
  }
  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    REP(as.length)(i => s += as(i))
    s
  }
  def cumSum(as: Array[Int]): Array[Long] = {
    val cum = Array.ofDim[Long](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

object Workspace {
  import Main._
}

class Main(out: java.io.PrintWriter, sc: Main.InputReader) {
  import sc._
  import Main._
  import java.util.Arrays.sort

  import scala.collection.mutable
  import math.{abs, max, min}
  import mutable.ArrayBuffer
  import Workspace._

  // toIntとか+7とかするならvalにしろ
  final private[this] val MOD = 1000000007

  def solve(): Unit = {
    REP(ni()) { _ =>
      val N = ni()
      val P = na(N)
      val med = N / 2
      var g, s, b = 0
      var state = 0
      REP(N / 2) { i =>
        if (i > 0 && P(i) != P(i - 1)) {
          state = state match {
            case 0 => 1
            case 1 if g < s => 2
            case 1 if g >= s => 1
            case 2 => 2
          }
        }
        state match {
          case 0 => g += 1
          case 1 => s += 1
          case 2 => if (P(i) > P(med)) b += 1
        }
      }
      if (g > 0 && s > 0 && b > 0 && g < s && g < b) {
        out.println(s"$g $s $b")
      } else {
        out.println("0 0 0")
      }
    }
  }
}


You are given an integer $$$n$$$. In $$$1$$$ move, you can do one of the following actions:  erase any digit of the number (it's acceptable that the number before the operation has exactly one digit and after the operation, it is "empty");  add one digit to the right. The actions may be performed in any order any number of times.Note that if, after deleting some digit from a number, it will contain leading zeroes, they will not be deleted. E.g. if you delete from the number $$$301$$$ the digit $$$3$$$, the result is the number $$$01$$$ (not $$$1$$$).You need to perform the minimum number of actions to make the number any power of $$$2$$$ (i.e. there's an integer $$$k$$$ ($$$k \ge 0$$$) such that the resulting number is equal to $$$2^k$$$). The resulting number must not have leading zeroes.E.g. consider $$$n=1052$$$. The answer is equal to $$$2$$$. First, let's add to the right one digit $$$4$$$ (the result will be $$$10524$$$). Then let's erase the digit $$$5$$$, so the result will be $$$1024$$$ which is a power of $$$2$$$.E.g. consider $$$n=8888$$$. The answer is equal to $$$3$$$. Let's erase any of the digits $$$8$$$ three times. The result will be $$$8$$$ which is a power of $$$2$$$.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n \le 10^9$$$).

For each test case, output in a separate line one integer $$$m$$$ — the minimum number of moves to transform the number into any power of $$$2$$$.

NoteThe answer for the first test case was considered above.The answer for the second test case was considered above.In the third test case, it's enough to add to the right the digit $$$4$$$ — the number $$$6$$$ will turn into $$$64$$$.In the fourth test case, let's add to the right the digit $$$8$$$ and then erase $$$7$$$ and $$$5$$$ — the taken number will turn into $$$8$$$.The numbers of the fifth and the sixth test cases are already powers of two so there's no need to make any move.In the seventh test case, you can delete first of all the digit $$$3$$$ (the result is $$$01$$$) and then the digit $$$0$$$ (the result is $$$1$$$).

import java.io._
import java.util.Scanner
import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
import util.control.Breaks._

object Solution {
    val reader = new Scanner(System.in)
    val writer =  new PrintWriter(System.out)

    def pow(a: Int, b: Int): Long = {
        if (b == 0) return 1

        val rs = pow(a, b / 2)
        rs * rs * (if (b % 2 == 0) 1 else a)
    }

    def GCD(a: Int, b: Int): Int = {
        if (a == 0) return b
        GCD(b % a, a)
    }

    def LCM(a: Int, b: Int): Int = (a / GCD(a,b)) * b

    def root(u: Int, p: Array[Int]): Int = {
        if (p(u) < 0) return u
        p(u) = root(p(u), p)
        p(u)
    }

    def join(u: Int, v: Int, p: Array[Int]): Unit = {
        var x = root(u, p)
        var y = root(v, p)
        if (x == y) return

        if (p(y) < p(x)) {
            val t = x
            x = y
            y = t
        }
        p(x) += p(y)
        p(y) = x
    }

    def transform(a: Array[Char], b: Array[Char]): Int = {
        var i = 0
        var j = 0

        var c = 0
        while (i < a.length) {
            while (j < b.length && a(i) != b(j)) {
                j += 1
                c += 1
            }
            if (j < b.length) {
                i += 1
                j += 1
                if (j == b.length)
                    return c + a.length - i
            } else return c + a.length - i
        }
        c + b.length - j
    }

    def solve(t: Int = 0): Unit = {
        val n = reader.next().toCharArray
        var min = Int.MaxValue
        //var rs: Array[Char] = null
        for (num <- f) {
            //val c = transform(num, n)
            /*if (min > c)
                rs = num*/
            min = Math.min(min, transform(num, n))
        }
        //println(rs.mkString)
        writer.println(min)
    }

    def multiTest(f: Int => Unit): Unit = {
        val t = reader.nextInt()
        for (i <- 1 to t) f(i)
    }

    val f = new Array[Array[Char]](101)
    def main(args: Array[String]) {
        for (i <- 0 to 100)
            f(i) = (1L << i).toString.toCharArray
        multiTest(solve)
        //solve()
        writer.flush()
        //println(transform(2.toString.toCharArray, 1052.toString.toCharArray))
    }
}


You are given a sequence of positive integers a1, a2, ..., an. Find all such indices i, that the i-th element equals the arithmetic mean of all other elements (that is all elements except for this one).

The first line contains the integer n (2 ≤ n ≤ 2·105). The second line contains elements of the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000). All the elements are positive integers.

Print on the first line the number of the sought indices. Print on the second line the sought indices in the increasing order. All indices are integers from 1 to n. If the sought elements do not exist, then the first output line should contain number 0. In this case you may either not print the second line or print an empty line.

import scala.collection.mutable.ListBuffer

object Main {

    def main(args: Array[String]) {
        val n = nextInt
        val lst = for (i <- 1 to n) yield nextInt
        val s = lst.sum
        val lb = new ListBuffer[Int]()
        for ((ai, i) <- lst zip (1 to n) ; if ai * n == s)
            lb += i
        println(lb.length)
        println(lb.mkString(" "))
    }

    val in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
    var st = new java.util.StringTokenizer("");

    def next() = {
        while (!st.hasMoreTokens())
            st = new java.util.StringTokenizer(in.readLine());
        st.nextToken();
    }

    def nextInt() = java.lang.Integer.parseInt(next());
    def nextDouble() = java.lang.Double.parseDouble(next());
    def nextLong() = java.lang.Long.parseLong(next());
}

object App extends App {
  Round3.print(Round3.solve(readLine(), readLine()))
}

object Round3 {
  def print(r: Result): Unit = {
    println(r.n)
    println(r.indices.mkString(" "))
  }

  case class Result(n: Int, indices: Array[Int])

  def solve(i1: String, i2: String): Result = {
    val nums = i2.split(" ").map { _.toDouble }
    val length = i1.toDouble
    val ave = nums.sum / length
    val indices = nums.zipWithIndex collect { case (n, i) if n == ave => i+1 }
    Result(indices.length, indices)
  }

}


Captain Marmot wants to prepare a huge and important battle against his enemy, Captain Snake. For this battle he has n regiments, each consisting of 4 moles.Initially, each mole i (1 ≤ i ≤ 4n) is placed at some position (xi, yi) in the Cartesian plane. Captain Marmot wants to move some moles to make the regiments compact, if it's possible.Each mole i has a home placed at the position (ai, bi). Moving this mole one time means rotating his position point (xi, yi) 90 degrees counter-clockwise around it's home point (ai, bi).A regiment is compact only if the position points of the 4 moles form a square with non-zero area.Help Captain Marmot to find out for each regiment the minimal number of moves required to make that regiment compact, if it's possible.

The first line contains one integer n (1 ≤ n ≤ 100), the number of regiments. The next 4n lines contain 4 integers xi, yi, ai, bi ( - 104 ≤ xi, yi, ai, bi ≤ 104).

Print n lines to the standard output. If the regiment i can be made compact, the i-th line should contain one integer, the minimal number of required moves. Otherwise, on the i-th line print "-1" (without quotes).

NoteIn the first regiment we can move once the second or the third mole.We can't make the second regiment compact.In the third regiment, from the last 3 moles we can move once one and twice another one.In the fourth regiment, we can move twice the first mole and once the third mole.

object CaptainMarmot extends App {

  import scala.io.StdIn

  def readAxis: (Int, Int, Int, Int) = {
    val Array(a, b, c, d) = StdIn.readLine().split(' ').map(_.toInt)
    (a, b, c, d)
  }

  val n = StdIn.readLine().toInt
  val moles: Seq[Seq[(Int, Int, Int, Int)]] = (1 to n).map(_ => Seq.fill(4)(readAxis)).toSeq

  moles.foreach(xs => println(CaptainMarmot(xs).solve))
}

case class CaptainMarmot(moles: Seq[(Int, Int, Int, Int)]) {
  type Coord = (Int, Int)

  def solve: Int = {
    val scores = (for {
      (s1, c1) <- rotates(moles(0))
      (s2, c2) <- rotates(moles(1))
      (s3, c3) <- rotates(moles(2))
      (s4, c4) <- rotates(moles(3))
    } yield {
      (s1 + s2 + s3 + s4, isSquare(c1, c2, c3, c4))
    }).filter(_._2).map(_._1)
    if (scores.isEmpty) -1 else scores.min
  }

  private[this] def rotates(xs: (Int, Int, Int, Int)): Seq[(Int, Coord)] = {
    val (homeX, homeY, centerX, centerY) = xs
    Seq(
      (0, (homeX, homeY)),
      (1, (centerX - homeY + centerY, centerY + homeX - centerX)),
      (2, (centerX * 2 - homeX, centerY * 2 - homeY)),
      (3, (centerX + homeY - centerY, centerY - homeX + centerX))
    )
  }

  private[this] def isSquare(c1: Coord, c2: Coord, c3: Coord, c4: Coord): Boolean = {
    val sets = Set(c1, c2, c3, c4).map{ case (x, y) => (x * 4, y * 4) }
    if (sets.size != 4) {
      false
    } else {
      val (x, y) = sets.head
      val rs = rotates(x, y, c1._1 + c2._1 + c3._1 + c4._1, c1._2 + c2._2 + c3._2 + c4._2).map(_._2)
      rs.toSet == sets
    }
  }
}

object Main {
  def main(args: Array[String]): Unit = {
    val s = new Main()
    s.solve()
    s.out.flush()
  }
}

class Main {
  import java.io._
  import java.util.StringTokenizer

  import scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

  val MOD = 1000000007
  val out = new PrintWriter(System.out)

  case class Coord(x: Int, y: Int) {
    def ccw: Coord = Coord(-y, x)
    def +(o: Coord) = Coord(x + o.x, y + o.y)
  }
  case class Mole(v: Coord, center: Coord) {
    def relative: Coord = Coord(v.x - center.x, v.y - center.y) // 中心からの相対位置
  }

  def solve(): Unit = {

    def readMole(): Mole = {
      Mole(Coord(ni(), ni()), Coord(ni(), ni()))
    }

    def ccw(m: Mole): Seq[(Coord, Int)] = {
      val res = Array.ofDim[(Coord, Int)](4)
      var cur = m.relative
      var i = 0
      while (i < 4) {
        res(i) = (cur + m.center, i)
        cur = cur.ccw
        i += 1
      }
      res
    }

    def isSquare(p1: Coord, p2: Coord, p3: Coord, p4: Coord): Boolean = {
      val nums = mutable.Map[Long, Int]().withDefaultValue(0)
      val ps = Array(p1, p2, p3, p4)
      REP(4) { i =>
        i + 1 until 4 foreach { j =>
          val a = ps(i)
          val b = ps(j)
          val x = abs(a.x - b.x)
          val y = abs(a.y - b.y)
          val dist = x.toLong * x + y.toLong * y // ２乗の形を維持する
          nums(dist) = nums(dist) + 1
        }
      }

//      val a = nums.toSeq.sortBy(_._1).map(_._2)
////      System.err.println(nums.toSeq.sortBy(_._1).map(_._2))
//      if (a == Seq(2, 2, 2) || a == Seq(2, 4)) {
//        System.err.println(nums.toSeq.sortBy(_._1).map(_._2))
//        System.err.println(nums)
//      }

      nums.size == 2 && nums.toSeq.sortBy(_._1).map(_._2) == Seq(4, 2)
    }

    REP(ni()) { _ =>
      val m1, m2, m3, m4 = readMole()
      val ans = for {
        (p1, c1) <- ccw(m1)
        (p2, c2) <- ccw(m2)
        (p3, c3) <- ccw(m3)
        (p4, c4) <- ccw(m4)
        if isSquare(p1, p2,p3, p4)
      } yield c1 + c2 + c3 + c4

      if (ans.isEmpty) out.println(-1)
      else out.println(ans.min)
    }
  }

  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next().toInt
    def nextLong(): Long = next().toLong
    def nextChar(): Char = next().charAt(0)
  }
  val sc = new InputReader(System.in)
  def ni(): Int = sc.nextInt()
  def nl(): Long = sc.nextLong()
  def nc(): Char = sc.nextChar()
  def ns(): String = sc.next()
  def ns(n: Int): Array[Char] = ns().toCharArray
  def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
  def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
    val A1, A2 = Array.ofDim[Int](n)
    REP(n) { i =>
      A1(i) = ni() + offset
      A2(i) = ni() + offset
    }
    (A1, A2)
  }
  def nm(n: Int, m: Int): Array[Array[Int]] = {
    val A = Array.ofDim[Int](n, m)
    REP(n) { i =>
      REP(m) { j =>
        A(i)(j) = ni()
      }
    }
    A
  }
  def nal(n: Int): Array[Long] = map(n)(_ => nl())
  def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }

  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    REP(as.length)(i => s += as(i))
    s
  }

  def cumSum(as: Array[Int]) = {
    val cum = Array.ofDim[Long](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

import scala.annotation.tailrec

object    CF271_3 extends App {
  case class Mole(x:Long, y:Long, a:Long, b:Long)

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readInts(n: Int) = {
    val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt)
  }
  def readLongs(n: Int) = {
    val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong)
  }

  def rotate(x:Long, y:Long, a:Long, b:Long):(Long, Long) = {
    ((a + b - y), (x - a +b))
  }

  def rotateMole(mole:Mole, n:Int):(Long,Long) = {
    var vp = (mole.x, mole.y)
    for(i <- 0 until n) {
      vp = rotate(vp._1, vp._2, mole.a, mole.b)
    }
    vp
  }

  def distance (p1: (Long, Long), p2: (Long, Long)) = {
    val (p1x, p1y) = p1
    val (p2x, p2y) = p2
    val dx = p1x - p2x
    val dy = p1y - p2y
    Math.sqrt(dx*dx + dy*dy)
  }

  def checkSqr(points:Array[(Long, Long)]):Boolean = {
    val Array(x,y,w,z) = points
    val yd = distance(x, y)
    val wd = distance(x, w)
    val zd = distance(x, z)
    if(yd == 0.0 || wd == 0.0 || zd == 0.0) {
      return false
    }
    val (s1, s2, s3) = if(yd == zd) {
      (y,z,w)
    } else if (wd == zd) {
      (w, z, y)
    } else if (yd == wd) {
      (y, w, z)
    } else {
      return false
    }
    if(distance(s2, s3) != distance(s1, s3)) {
      return false
    }

    distance(x, s2) == distance(s2, s3) && distance(x, s3) == distance(s1, s2)
    //distance(x, s3) == distance(s1, s2) &&
  }

  val Array(n) = readInts(1)
  for(testcase <- 0 until n) {
    val moles = (1 to 4).toArray.map(_ => {
      val Array(x, y, a, b) = readLongs(4)
      val mole = Mole(x, y, a, b)
      mole
    })

    var sol = -1
    for(r1 <- 0 to 3; r2 <- 0 to 3; r3 <- 0 to 3; r4 <- 0 to 3) {
      val sum = r1 + r2 + r3 + r4
      if((sol >= 0 && sum < sol) || sol == -1){
        val points = Array(
          rotateMole(moles(0), r1),
          rotateMole(moles(1), r2),
          rotateMole(moles(2), r3),
          rotateMole(moles(3), r4)
        )
        if(checkSqr(points)){
          //println(r1,r2,r3,r4)
          sol = sum
        }
      }
    }
    println(sol)
  }

}

object Main {
  def main(args: Array[String]): Unit = {
    val s = new Main()
    s.solve()
    s.out.flush()
  }
}

class Main {
  import java.io._
  import java.util.StringTokenizer

  import scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

  val MOD = 1000000007
  val out = new PrintWriter(System.out)

  case class Coord(x: Int, y: Int) {
    def ccw: Coord = Coord(-y, x)
    def +(o: Coord) = Coord(x + o.x, y + o.y)
  }
  case class Mole(v: Coord, center: Coord) {
    def relative: Coord = Coord(v.x - center.x, v.y - center.y) // 中心からの相対位置
  }

  def solve(): Unit = {

    def readMole(): Mole = {
      Mole(Coord(ni(), ni()), Coord(ni(), ni()))
    }

    def ccw(m: Mole): Seq[(Coord, Int)] = {
      val res = Array.ofDim[(Coord, Int)](4)
      var cur = m.relative
      var i = 0
      while (i < 4) {
        res(i) = (cur + m.center, i)
        cur = cur.ccw
        i += 1
      }
      res
    }

    def isSquare(p1: Coord, p2: Coord, p3: Coord, p4: Coord): Boolean = {
      val nums = mutable.Map[Int, Int]().withDefaultValue(0)
      val ps = Array(p1, p2, p3, p4)
      REP(4) { i =>
        i + 1 until 4 foreach { j =>
          val a = ps(i)
          val b = ps(j)
          val x = abs(a.x - b.x)
          val y = abs(a.y - b.y)
          val dist = x * x + y * y // ２乗の形を維持する
          nums(dist) = nums(dist) + 1
        }
      }
      nums.size == 2 && nums.toSeq.sortBy(_._1).map(_._2) == Seq(4, 2)
    }

    REP(ni()) { _ =>
      val m1, m2, m3, m4 = readMole()
      val ans = for {
        (p1, c1) <- ccw(m1)
        (p2, c2) <- ccw(m2)
        (p3, c3) <- ccw(m3)
        (p4, c4) <- ccw(m4)
        if isSquare(p1, p2,p3, p4)
      } yield c1 + c2 + c3 + c4

      if (ans.isEmpty) out.println(-1)
      else out.println(ans.min)
    }
  }

  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next().toInt
    def nextLong(): Long = next().toLong
    def nextChar(): Char = next().charAt(0)
  }
  val sc = new InputReader(System.in)
  def ni(): Int = sc.nextInt()
  def nl(): Long = sc.nextLong()
  def nc(): Char = sc.nextChar()
  def ns(): String = sc.next()
  def ns(n: Int): Array[Char] = ns().toCharArray
  def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
  def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
    val A1, A2 = Array.ofDim[Int](n)
    REP(n) { i =>
      A1(i) = ni() + offset
      A2(i) = ni() + offset
    }
    (A1, A2)
  }
  def nm(n: Int, m: Int): Array[Array[Int]] = {
    val A = Array.ofDim[Int](n, m)
    REP(n) { i =>
      REP(m) { j =>
        A(i)(j) = ni()
      }
    }
    A
  }
  def nal(n: Int): Array[Long] = map(n)(_ => nl())
  def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }

  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    REP(as.length)(i => s += as(i))
    s
  }

  def cumSum(as: Array[Int]) = {
    val cum = Array.ofDim[Long](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

object Main {
  def main(args: Array[String]): Unit = {
    val s = new Main()
    s.solve()
    s.out.flush()
  }
}

class Main {
  import java.io._
  import java.util.StringTokenizer

  import scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

  val MOD = 1000000007
  val out = new PrintWriter(System.out)

  case class Coord(x: Int, y: Int) {
    def ccw: Coord = Coord(-y, x) // todo

    def +(o: Coord) = Coord(x + o.x, y + o.y)
  }
  case class Mole(v: Coord, center: Coord) {
    def relative: Coord = Coord(v.x - center.x, v.y - center.y) // 中心からの相対位置
  }

  def solve(): Unit = {

    def readMole(): Mole = {
      Mole(Coord(ni(), ni()), Coord(ni(), ni()))
    }

    def ccw(m: Mole): Seq[(Coord, Int)] = {
      val res = Array.ofDim[(Coord, Int)](4)
      var cur = m.relative
      var i = 0
      while (i < 4) {
        res(i) = (cur + m.center, i)
        cur = cur.ccw
        i += 1
      }
      res
    }

    def isSquare(p1: Coord, p2: Coord, p3: Coord, p4: Coord): Boolean = {
      val nums = mutable.Map[Int, Int]().withDefaultValue(0)
      val ps = Array(p1, p2, p3, p4)
      REP(4) { i =>
        i + 1 until 4 foreach { j =>
          val a = ps(i)
          val b = ps(j)
          val x = abs(a.x - b.x)
          val y = abs(a.y - b.y)
          val dist = x * x + y * y // ２乗の形を維持する
          nums(dist) = nums(dist) + 1
        }
      }
      nums.size == 2 && nums.values.toSet == Set(2, 4)
    }

    REP(ni()) { _ =>
      val m1, m2, m3, m4 = readMole()
      val ans = for {
        (p1, c1) <- ccw(m1)
        (p2, c2) <- ccw(m2)
        (p3, c3) <- ccw(m3)
        (p4, c4) <- ccw(m4)
        if isSquare(p1, p2,p3, p4)
      } yield c1 + c2 + c3 + c4

      if (ans.isEmpty) out.println(-1)
      else out.println(ans.min)
    }
  }

  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next().toInt
    def nextLong(): Long = next().toLong
    def nextChar(): Char = next().charAt(0)
  }
  val sc = new InputReader(System.in)
  def ni(): Int = sc.nextInt()
  def nl(): Long = sc.nextLong()
  def nc(): Char = sc.nextChar()
  def ns(): String = sc.next()
  def ns(n: Int): Array[Char] = ns().toCharArray
  def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
  def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
    val A1, A2 = Array.ofDim[Int](n)
    REP(n) { i =>
      A1(i) = ni() + offset
      A2(i) = ni() + offset
    }
    (A1, A2)
  }
  def nm(n: Int, m: Int): Array[Array[Int]] = {
    val A = Array.ofDim[Int](n, m)
    REP(n) { i =>
      REP(m) { j =>
        A(i)(j) = ni()
      }
    }
    A
  }
  def nal(n: Int): Array[Long] = map(n)(_ => nl())
  def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }

  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    REP(as.length)(i => s += as(i))
    s
  }

  def cumSum(as: Array[Int]) = {
    val cum = Array.ofDim[Long](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

import scala.annotation.tailrec

object    CF271_3 extends App {
  case class Mole(x:Long, y:Long, a:Long, b:Long)

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readInts(n: Int) = {
    val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt)
  }
  def readLongs(n: Int) = {
    val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong)
  }

  def rotate(x:Long, y:Long, a:Long, b:Long):(Long, Long) = {
    ((a + b - y), (x - a +b))
  }

  def rotateMole(mole:Mole, n:Int):(Long,Long) = {
    var vp = (mole.x, mole.y)
    for(i <- 0 until n) {
      vp = rotate(vp._1, vp._2, mole.a, mole.b)
    }
    vp
  }

  def distance (p1: (Long, Long), p2: (Long, Long)) = {
    val (p1x, p1y) = p1
    val (p2x, p2y) = p2
    val dx = p1x - p2x
    val dy = p1y - p2y
    Math.sqrt(dx*dx + dy*dy)
  }

  def checkSqr(points:Array[(Long, Long)]):Boolean = {
    val Array(x,y,w,z) = points
    val yd = distance(x, y)
    val wd = distance(x, w)
    val zd = distance(x, z)
    if(yd == 0.0 || wd == 0.0 || zd == 0.0) {
      return false
    }
    val (s1, s2, s3) = if(yd == zd) {
      (y,z,w)
    } else if (wd == zd) {
      (w, z, y)
    } else if (yd == wd) {
      (y, w, z)
    } else {
      return false
    }
    if(distance(s2, s3) != distance(s1, s3)) {
      return false
    }
    distance(x, s3) == distance(s1, s2)
  }

  val Array(n) = readInts(1)
  for(testcase <- 0 until n) {
    val moles = (1 to 4).toArray.map(_ => {
      val Array(x, y, a, b) = readLongs(4)
      val mole = Mole(x, y, a, b)
      mole
    })

    var sol = -1
    for(r1 <- 0 to 3; r2 <- 0 to 3; r3 <- 0 to 3; r4 <- 0 to 3) {
      val sum = r1 + r2 + r3 + r4
      if((sol >= 0 && sum < sol) || sol == -1){
        val points = Array(
          rotateMole(moles(0), r1),
          rotateMole(moles(1), r2),
          rotateMole(moles(2), r3),
          rotateMole(moles(3), r4)
        )
        if(checkSqr(points)){
          sol = sum
        }
      }
    }
    println(sol)
  }

}

import scala.annotation.tailrec

object    CF271_3 extends App {
  case class Mole(x:Int, y:Int, a:Int, b:Int)

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readInts(n: Int) = {
    val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt)
  }

  def rotate(x:Int, y:Int, a:Int, b:Int):(Int, Int) = {
    ((a + b - y), (x - a +b))
  }

  def rotateMole(mole:Mole, n:Int):(Int,Int) = {
    var vp = (mole.x, mole.y)
    for(i <- 0 until n) {
      vp = rotate(vp._1, vp._2, mole.a, mole.b)
    }
    vp
  }

  def distance (p1: (Int, Int), p2: (Int, Int)) = {
    val (p1x, p1y) = p1
    val (p2x, p2y) = p2
    val dx = p1x - p2x
    val dy = p1y - p2y
    Math.sqrt(dx*dx + dy*dy)
  }

  def checkSqr(points:Array[(Int, Int)]):Boolean = {
    val Array(x,y,w,z) = points
    val yd = distance(x, y)
    val wd = distance(x, w)
    val zd = distance(x, z)
    if(yd == 0.0 || wd == 0.0 || zd == 0.0) {
      return false
    }
    val (s1, s2, s3) = if(yd == zd) {
      (y,z,w)
    } else if (wd == zd) {
      (w, z, y)
    } else if (yd == wd) {
      (y, w, z)
    } else {
      return false
    }
    if(distance(s2, s3) != distance(s1, s3)) {
      return false
    }
    distance(x, s3) == distance(s1, s2)
  }

  val Array(n) = readInts(1)
  for(testcase <- 0 until n) {
    val moles = (1 to 4).toArray.map(_ => {
      val Array(x, y, a, b) = readInts(4)
      val mole = Mole(x, y, a, b)
      mole
    })

    var sol = -1
    for(r1 <- 0 to 3; r2 <- 0 to 3; r3 <- 0 to 3; r4 <- 0 to 3) {
      val sum = r1 + r2 + r3 + r4
      if((sol >= 0 && sum < sol) || sol == -1){
        val points = Array(
          rotateMole(moles(0), r1),
          rotateMole(moles(1), r2),
          rotateMole(moles(2), r3),
          rotateMole(moles(3), r4)
        )
        if(checkSqr(points)){
          sol = sum
        }
      }
    }
    println(sol)
  }

}


Little Vasya's uncle is a postman. The post offices are located on one circular road. Besides, each post office has its own gas station located next to it. Petya's uncle works as follows: in the morning he should leave the house and go to some post office. In the office he receives a portion of letters and a car. Then he must drive in the given car exactly one round along the circular road and return to the starting post office (the uncle can drive along the circle in any direction, counterclockwise or clockwise). Besides, since the car belongs to the city post, it should also be fuelled with gasoline only at the Post Office stations. The total number of stations equals to n. One can fuel the car at the i-th station with no more than ai liters of gasoline. Besides, one can fuel the car no more than once at each station. Also, the distance between the 1-st and the 2-nd station is b1 kilometers, the distance between the 2-nd and the 3-rd one is b2 kilometers, ..., between the (n - 1)-th and the n-th ones the distance is bn - 1 kilometers and between the n-th and the 1-st one the distance is bn kilometers. Petya's uncle's high-tech car uses only one liter of gasoline per kilometer. It is known that the stations are located so that the sum of all ai is equal to the sum of all bi. The i-th gas station and i-th post office are very close, so the distance between them is 0 kilometers.Thus, it becomes clear that if we start from some post offices, then it is not always possible to drive one round along a circular road. The uncle faces the following problem: to what stations can he go in the morning to be able to ride exactly one circle along the circular road and visit all the post offices that are on it?Petya, who used to attend programming classes, has volunteered to help his uncle, but his knowledge turned out to be not enough, so he asks you to help him write the program that will solve the posed problem.

The first line contains integer n (1 ≤ n ≤ 105). The second line contains n integers ai — amount of gasoline on the i-th station. The third line contains n integers b1, b2, ..., bn. They are the distances between the 1-st and the 2-nd gas stations, between the 2-nd and the 3-rd ones, ..., between the n-th and the 1-st ones, respectively. The sum of all bi equals to the sum of all ai and is no more than 109. Each of the numbers ai, bi is no less than 1 and no more than 109.

Print on the first line the number k — the number of possible post offices, from which the car can drive one circle along a circular road. Print on the second line k numbers in the ascending order — the numbers of offices, from which the car can start.

object P066E extends App {
  def ps(a:List[Int])={
    a.scan(0)(_+_).drop(1)
  }
  def calc(a:List[Int], b:List[Int], n:Int):Set[Int] = {
    val c = (ps(a) zip ps(b)).map(x=>x._1-x._2)
    val x = c.min
    val d = c.zipWithIndex.filter(_._1==x).map(x=>(x._2+1)%n)
    return d.toSet
  }

  val n = readInt
  val a = readLine.split(' ').map(_.toInt).toList
  val b = readLine.split(' ').map(_.toInt).toList
  val s1 = calc(a, b, n)
  val s2 = calc(a.reverse, (b.takeRight(1):::b.take(n-1)).reverse, n).map(x=>n-1-x)
  val s = s1.union(s2).map(x=>x+1)
  println(s.size)
  println(s.toArray.sorted.deep.mkString(" "))
}


After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which  is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on.

The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly.

Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball.

import java.util.Scanner

object Main {

   def main(args: Array[String]): Unit = {
       val scanner = new Scanner (System.in)
		 val n, m = scanner.nextInt()
		 val ansList = List.iterate ((m + 1) / 2, m) { i =>
			 if (i <= m / 2) m - i + 1 else m - i
		 }
		 val ans = ansList.toArray
		 for (i <- 0 to n-1) {
			 println (ans(i%m))
		 }
   }

}

import java.util.Scanner

object Main {

   def main(args: Array[String]): Unit = {
       val scanner = new Scanner (System.in)
		 val n, m = scanner.nextInt()
		 val ansList = List.iterate ((m + 1) / 2, m) { i =>
		 	 val even = m % 2 == 0
			 val big  = i > m / 2
			 if (even == big) m - i + 1 else m - i
		 }
		 val ans = ansList.toArray
		 for (i <- 0 to n-1) {
			 println (ans(i%m))
		 }
   }

}


There always is something to choose from! And now, instead of "Noughts and Crosses", Inna choose a very unusual upgrade of this game. The rules of the game are given below:There is one person playing the game. Before the beginning of the game he puts 12 cards in a row on the table. Each card contains a character: "X" or "O". Then the player chooses two positive integers a and b (a·b = 12), after that he makes a table of size a × b from the cards he put on the table as follows: the first b cards form the first row of the table, the second b cards form the second row of the table and so on, the last b cards form the last (number a) row of the table. The player wins if some column of the table contain characters "X" on all cards. Otherwise, the player loses.Inna has already put 12 cards on the table in a row. But unfortunately, she doesn't know what numbers a and b to choose. Help her win the game: print to her all the possible ways of numbers a, b that she can choose and win.

The first line of the input contains integer t (1 ≤ t ≤ 100). This value shows the number of sets of test data in the input. Next follows the description of each of the t tests on a separate line. The description of each test is a string consisting of 12 characters, each character is either "X", or "O". The i-th character of the string shows the character that is written on the i-th card from the start.

For each test, print the answer to the test on a single line. The first number in the line must represent the number of distinct ways to choose the pair a, b. Next, print on this line the pairs in the format axb. Print the pairs in the order of increasing first parameter (a). Separate the pairs in the line by whitespaces.

4
OXXXOXOOXOOX
OXOXOXOXOXOX
XXXXXXXXXXXX
OOOOOOOOOOOO

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val n = in.next().toInt
  val res = (1 to n).map { _ =>
    val str = in.next()
    val res = List(1, 2, 3, 4, 6, 12).filter{i =>
      (0 until i).exists{j =>
        (j until 12 by i).forall{k => str(k ) == 'X'}
      }
    }.map(i => s"${12 / i}x$i").reverse
    s"${res.length} ${res.mkString(" ")}"

  }
  println(res.mkString("\n"))
}

object A400 {
  @inline def tokenizeLine = new java.util.StringTokenizer(scala.io.StdIn.readLine)

  def readInts(n: Int) = {
    val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt)
  }

  def readLongs(n: Int) = {
    val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong)
  }

  def readBigs(n: Int) = {
    val tl = tokenizeLine; Array.fill(n)(BigInt(tl.nextToken))
  }

  def main(args: Array[String]): Unit = {
    val Array(tests) = readInts(1)
    for (_ <- 0 until tests) {
      val input = scala.io.StdIn.readLine
      val size = input.length
      var res = 0
      var arr = Array[String]()
      for(i <- 1 to size) {
        if(size % i == 0 && solve(input, i)) {
          res += 1
          arr ++= Array(s"${i}x${size/i}")
        }
      }
      println(res + arr.mkString(" ", " ", ""))
    }
  }
  def solve(input: String, rows: Int): Boolean = {
    val col = input.length / rows
    val in = input.toArray.grouped(col).toArray
    (0 until col).exists(c => (0 until rows).forall(r => in(r)(c) == 'X'))
  }
}

import java.util.Scanner
import java.io.PrintWriter
import scala.annotation.tailrec
import scala.collection.mutable
import scala.util.Random

object P400A extends App {
  val sc = new Scanner(System.in)
  val out = new PrintWriter(System.out)

  val N = sc.nextInt
  sc.nextLine

  // 1x12, 2x6, 3x4, 4x3, 6x2, 12x1

  class TestCase {
    val cards = sc.nextLine.toList

    def solve(): List[String] = {

      @inline
      def isWinning(hand: List[Char], rowSize: Int): Boolean = {
        hand.sliding(rowSize, rowSize).toList.
          transpose.find(row => row.forall(_ == 'X')) match {
          case Some(_) => true
          case None    => false
        }
      }
      val rowSize = List(12, 6, 4, 3, 2, 1)
      rowSize.filter(isWinning(cards, _)).map { i =>
        (12 / i).toString + "x" + i.toString
      }
    }
  }

  for (_ <- 0 until N) {
    val answer = (new TestCase).solve
    val line = answer.size :: answer
    out.println(line.mkString(" "))
  }
  out.close
}

/**
 * Created by hama_du on 2014/02/24.
 */
object ProblemA extends App {
  val T = readInt
  (0 until T).foreach(i => {
    println(solve(readLine.toCharArray))
  })

  def solve(in: Array[Char]): String = {
    val answers = Array((1,12),(2,6),(3,4),(4,3),(6,2),(12,1)).filter(p => {
      (0 until p._2).foldLeft(false)((a, b) => a || isAllX(p, in, b))
    })
    if (answers.size == 0) {
      return "0"
    }
    return answers.size + " " +  answers.map(p => p._1 + "x" + p._2).mkString(" ")
  }

  def isAllX(p: (Int,Int), in: Array[Char], b: Int) = {
    (b until in.size by p._2).filter(idx => in(idx) == 'X').size == p._1
  }
}

object A extends App {
  val n = readInt()
  val in = Array.fill(n)(readLine())
  val ls = for (i <- 1 to 12; j <- 1 to 12; if (i*j==12)) yield (j, i)
  for (s <- in) {
    var q = List[(Int,Int)]()
    for (c <- ls) {
      var yes = false;
      for (i <- 0 until c._2) {
        val t = for (j <- 0 until c._1) yield s(i + j*c._2);
        if (!yes) yes = !t.contains('O')
      }
      if (yes) q ::= c
    }

    print(q.length)
    for (t <- q) {
      print(" " + t._1 + "x" + t._2)
    }
    println();
  }
}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val n = in.next().toInt
  val res = (1 to n).map { _ =>
    val str = in.next()
    val res = List(1, 2, 3, 4, 6, 12).filter{i =>
      (0 until i).exists{j =>
        (j until 12 by i).forall{k => str(k ) == 'X'}
      }
    }.map(i => s"${i}x${12 / i}")
    s"${res.length} ${res.mkString(" ")}"

  }
  println(res.mkString("\n"))
}