Skip to content

Instantly share code, notes, and snippets.

@waynejo
Created January 15, 2016 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waynejo/dc119f3c097ec717131d to your computer and use it in GitHub Desktop.
Save waynejo/dc119f3c097ec717131d to your computer and use it in GitHub Desktop.
package Main
import java.io.{FileOutputStream, FileInputStream}
import scala.io.StdIn
object Main extends App {
Console.setIn(new FileInputStream("example.in"))
Console.setIn(new FileInputStream("A-large-practice.in"))
Console.setOut(new FileOutputStream("A-large-practice.out"))
def lcm(a:Long, b:Long) = a * b / gcd(a, b)
def gcd(a:Long, b:Long):Long = if (0 == b) a else gcd(b, a % b)
def solve(month:Long, nOfMonth:Long, nOfWeek:Long): Long = {
val remains = nOfMonth % nOfWeek
if (0 == remains) {
month * (nOfMonth / nOfWeek)
} else {
val nOfLeapMonth = lcm(remains, nOfWeek)
val additional = if (0 == (remains * month % nOfLeapMonth)) 1 else 0
val totalWeek = (month * nOfMonth + nOfWeek - 1) / nOfWeek
totalWeek + (month - 1) - (remains * month / nOfLeapMonth) + additional
}
}
val cases = StdIn.readLine().toInt
(1 to cases) foreach { num =>
val Array(month, nOfMonth, nOfWeek) = StdIn.readLine().split(" ").map(_.toLong)
println(s"Case #$num: ${solve(month, nOfMonth, nOfWeek)}")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment