Skip to content

Instantly share code, notes, and snippets.

Created January 9, 2015 13:26
Show Gist options
  • Save anonymous/fd6b073a404bbba83b6a to your computer and use it in GitHub Desktop.
Save anonymous/fd6b073a404bbba83b6a to your computer and use it in GitHub Desktop.
object manyPrizes {
def getMinWin(N: Int, P: Int): Int = {
val lastTeam = BigInt(2).pow(N).toInt - 1
(lastTeam to (lastTeam - P + 1) by -1).map(BigInt(_).bitCount).min
} //> getMinWin: (N: Int, P: Int)Int
def defeatTeamNum(numWin: Int): Int = BigInt(2).pow(numWin).toInt - 1
//> defeatTeamNum: (numWin: Int)Int
def maxTeamNum(N: Int, defeatNum: Int): Int = BigInt(2).pow(N).toInt - 1 - defeatNum
//> maxTeamNum: (N: Int, defeatNum: Int)Int
def getLuckyTeam(N: Int, P: Int): Int = maxTeamNum(N, defeatTeamNum(getMinWin(N, P)))
//> getLuckyTeam: (N: Int, P: Int)Int
def getStrongTeam(N: Int, P: Int): Int = 0 //> getStrongTeam: (N: Int, P: Int)Int
getMinWin(3, 4) //> res0: Int = 1
getMinWin(3, 5) //> res1: Int = 1
getMinWin(3, 3) //> res2: Int = 2
defeatTeamNum(1) //> res3: Int = 1
defeatTeamNum(2) //> res4: Int = 3
defeatTeamNum(3) //> res5: Int = 7
defeatTeamNum(4) //> res6: Int = 15
maxTeamNum(3, 1) //> res7: Int = 6
maxTeamNum(3, 3) //> res8: Int = 4
maxTeamNum(3, 7) //> res9: Int = 0
getLuckyTeam(3, 4) //> res10: Int = 6
getLuckyTeam(3, 5) //> res11: Int = 6
getLuckyTeam(3, 3) //> res12: Int = 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment