Created
January 15, 2016 12:31
-
-
Save waynejo/dc119f3c097ec717131d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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