import scala.annotation.tailrec import net.pushl.io.MyConsole package net.pushl { package number { // Prime (Prime.scala) // Number (Number.scala) // Combi (Combi.scala) // Optimize (Optimize.scala) } package string { // RollingHash (RollingHash.scala) } package geometry { // Point (Geometry.scala) // Segment (Geometry.scala) // Line (Geometry.scala) } package datastructure { // UnionFindTree (UnionFind.scala) } package io { } } package net.pushl.number { object Number { // 1,2,3 -> 1 | 4,5,6,7,8 -> 2 | 9... -> 3 def sqrt_ceil(n : Long) : Long = { val EPS = 1e-8 Math.sqrt(n.toDouble+EPS).toLong } // TODO write test final def doubling[T](x : T, n : Long, zero : T, bop : (T,T) => T) : T = if(n == 0) zero else if(n % 2 == 0) doubling(bop(x,x),n/2,zero,bop) else bop(doubling(bop(x,x),n/2,zero,bop),x) // TODO: Write test def gcd(a : Long, b : Long) : Long = { if(b == 0) a else gcd(b,a%b) } // TODO: Write test def lcm(a : Long, b : Long) : Long = { if(a < 0) lcm(-a,b) else if(b < 0) lcm(a,-b) else a*b / gcd(a,b) } } } // Document: http://www.scala-lang.org/api/current/#package // ----------------------------------------------------------------------------- import net.pushl.number.Number case class C(val x: Long, val y: Long) class Solver(val stdio: MyConsole){ import stdio._ // shadow Console.~ def main() : Unit = { val i = (1 to 3).map(_ => { val x,y = nextInt() C(x,y) }) val first = (0 to i(1).y.toInt).find(k => { val p = i(0).x + i(0).y * k p != 0 && p % i(1).y == i(1).x }) first match { case Some(k) => { val p = i(0).x + i(0).y * k val second = (0 to i(2).y.toInt).find(k => { val q = p + Number.lcm(i(0).y,i(1).y) * k q != 0 && q % i(2).y == i(2).x }) second match { case Some(k) => { println(p + Number.lcm(i(0).y,i(1).y) * k) } case None => { println("-1") } } } case None => { println("-1") } } } } // ----------------------------------------------------------------------------0 // ----------------------------------------------------------------------------0 object Main { def main(args: Array[String]) : Unit = { val console = new MyConsole(Console.in, Console.out, Console.err) val solver = new Solver(console) solver.main() console.flush() } } package net.pushl.io { import java.io.{BufferedReader, PrintWriter, PrintStream, PushbackReader} class MyConsole(val in: BufferedReader, val _out: PrintStream, val err: PrintStream) { // PrintWriter do not flush automatically val out = new PrintWriter(_out,false) // If argument is null, there are ambiguous which function will be called def print(obj: Any) = out.print(if(obj == null) "null" else obj.toString) def println() = out.println() def println(obj: Any) = out.println(obj) def printf(text: String, args: Any*) = out.printf(text.format(args : _*)) // NOTE: YOU MUST FLUSH BEFORE END OF MAIN def flush() = out.flush() def debugln(obj: Any) = err.println(obj) def readIntVector() : Vector[Int] = parseLineToVector(() => nextInt) def readLongVector() : Vector[Long] = parseLineToVector(() => nextLong) def readStringVector() : Vector[String] = parseLineToVector(() => nextString) def nextInt() : Int = nextLong().toInt def nextBigInt() : BigInt = BigInt(nextString()) def nextBigDecimal( ) : BigDecimal = BigDecimal(nextString()) def nextDouble() : Double = nextString().toDouble def nextLong() : Long = { if(!goNextValuable()) throw new NoSuchElementException("Reading long failed") val sgn = if(peek == '-') -1l else 1 if(sgn == -1l) read() if(peek < '0' || '9' < peek) throw new NumberFormatException(s"readLong found only '-' or no number") @tailrec def readLong(next: Int, cur: Long) : Long = if('0' <= next && next <= '9') readLong(readWithoutCheckingPeeked(), cur*10 + next-'0') else if(isEnd(next) || isSpaceOrControl(next)) sgn*cur else throw new NumberFormatException(s"readLong found strange byte $next") val res = readLong(read(),0) skipTrailingSpaces() res } def nextString() : String = { if(!goNextValuable()) throw new NoSuchElementException("Reading String failed") val builder = new StringBuilder @tailrec def appendCode(next: Int) : String = { if(isEnd(next) || isSpaceOrControl(next)){ builder.toString }else{ builder.append(next.toChar) appendCode(readWithoutCheckingPeeked()) } } val res = appendCode(read()) skipTrailingSpaces() res } // TODO: refactoring to marge nextString def readLine() : String = { if(isEnd(peek)) throw new NoSuchElementException("Reading Line failed") val builder = new StringBuilder @tailrec def appendCode(next: Int) : String = { if(isEnd(next) || isNewLine(next)){ builder.toString }else{ builder.append(next.toChar) appendCode(read()) } } appendCode(read()) } // helpers private[this] var peeked: Option[Int] = None private[this] var last = -1 private def read() = { val res = peeked match { case None => in.read() case Some(a) => { peeked = None; a } } last = res res } @inline private def readWithoutCheckingPeeked() = { val res = in.read() last = res res } @inline private def peek() = peeked match { case None => { val res = in.read() peeked = Some(res) res } case Some(a) => a } @inline private def isEnd(c: Int) = c == -1 @inline private def isNewLine(c: Int) = c == 10 || c == 13 // LF and CR @inline private def isThereReadable() = !isEnd(peek) // XXX: this limits c is ASCII? @inline private def isSpaceOrControl(c: Int) = (0 <= c && c <= 32) || c == 127 @tailrec final private def goNextNotSpaceNorControl() : Unit = if(isSpaceOrControl(peek)){ read() goNextNotSpaceNorControl() } final private def skipTrailingSpaces() : Unit = { @tailrec def skipTrailingSpacesAux() : Unit = { if(!isNewLine(last) && !isNewLine(peek) && isSpaceOrControl(peek)){ read() skipTrailingSpacesAux() } } skipTrailingSpacesAux if(!isNewLine(last) && isNewLine(peek)) {val _ = read()} } @tailrec final private def skipTrailingSpacesAndNewline() : Unit = if(isNewLine(peek)){ val _ = read() // windows causes error. maybe. }else if(isSpaceOrControl(peek)){ read() skipTrailingSpacesAndNewline() } @inline private def goNextValuable() = { goNextNotSpaceNorControl() isThereReadable() } @inline private def parseLineToVector[X](parser: () => X) : Vector[X] = { import scala.collection.immutable.VectorBuilder val vb = new VectorBuilder[X]() @tailrec def parseLineToVectorAux(first: Boolean=false) : Vector[X] = if((!first && isNewLine(last)) || isNewLine(peek) || isEnd(peek)) { vb.result }else{ vb += parser() parseLineToVectorAux() } parseLineToVectorAux(true) } } }