Skip to content

Instantly share code, notes, and snippets.

@felipecwb
Last active May 24, 2017 17:12
Show Gist options
  • Save felipecwb/ce67e63bf654e6e1306ac5d558bcc608 to your computer and use it in GitHub Desktop.
Save felipecwb/ce67e63bf654e6e1306ac5d558bcc608 to your computer and use it in GitHub Desktop.
Calculate different sequence of scores in a Volleyball match - https://www.hackerrank.com/contests/w1/challenges/volleyball-match
#!/usr/bin/env php
<?php
const MOD = 1e9 + 7;
function fact($n) {
return ($n > 1) ? $n * fact($n - 1) : 1;
}
function mpow($base, $exp, $mod) {
$r = 1;
for ($exp; $exp > 0; $exp--) {
$r = $r * $base % $mod;
}
return $r;
}
function volley($a, $b) {
list($a, $b) = $a > $b ? [$a, $b] : [$b, $a];
if ($a < 25
|| ($a == 25 and ($a - $b) < 2)
|| ($a > 25 && ($a - $b) != 2)
) {
return 0;
}
$exp = 0;
$a -= 1;
if ($a >= 25) {
$exp = $b - 24;
$a = $b = 24;
}
$c = floor(fact($a + $b) / (fact($a) * fact($b))) % MOD;
return $c * mpow(2, $exp, MOD) % MOD;
}
// main:
$a = (int) trim(fgets(STDIN));
$b = (int) trim(fgets(STDIN));
echo volley($a, $b) . PHP_EOL;
#!/usr/bin/env python3
from math import factorial
MOD = int(1e9 + 7);
def volley(a, b):
a, b = (a, b) if a > b else (b, a)
if a < 25 \
or (a <= 25 and (a-b) < 2) \
or (a > 25 and (a-b) != 2):
return 0
a -= 1
exp = 0
if a >= 25:
exp = b - 24
a = 24
b = 24
ans = factorial(a + b) // (factorial(a) * factorial(b)) % MOD
return ans * pow(2, exp, MOD) % MOD
if __name__ == '__main__':
a = int(input())
b = int(input())
print("%d" %volley(a, b))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment