mirror of
https://github.com/iritmaximus/tira.git
synced 2026-10-12 19:13:47 +03:00
53 lines
1.5 KiB
Python
53 lines
1.5 KiB
Python
from fractions import Fraction
|
|
import decimal
|
|
import math
|
|
from math import factorial
|
|
|
|
|
|
def calc_binomial(a: int, b: int) -> int:
|
|
a_fact = factorial(a)
|
|
b_fact = factorial(b)
|
|
a_b_fact = factorial(a - b)
|
|
denominator = b_fact * a_b_fact
|
|
|
|
# This has to be // for some reason...
|
|
result = a_fact // denominator
|
|
assert result == math.comb(a, b)
|
|
|
|
return result
|
|
|
|
|
|
def count_sequences(n: int):
|
|
if n % 2 != 0:
|
|
return 0
|
|
|
|
n_div = n // 2
|
|
assert n_div.is_integer()
|
|
|
|
binomial = calc_binomial(n, n_div)
|
|
assert binomial.is_integer()
|
|
|
|
# oh man this took time...
|
|
result_div = Fraction(1, (n_div + 1))
|
|
result = round(result_div * binomial)
|
|
|
|
return int(result)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
assert count_sequences(1) == 0
|
|
assert count_sequences(2) == 1
|
|
assert count_sequences(3) == 0
|
|
assert count_sequences(4) == 2
|
|
assert count_sequences(5) == 0
|
|
assert count_sequences(6) == 5
|
|
|
|
assert count_sequences(42) == 24466267020
|
|
assert count_sequences(62) == 14544636039226909
|
|
assert count_sequences(64) == 55534064877048198
|
|
assert (
|
|
count_sequences(1000)
|
|
== 539497486917039060909410566119711128734834348196703167679426896420410037336371644508208550747509720888947317534973145917768881736628103627844100238921194561723883202123256952806711505149177419849031086149939116975191706558395784192643914160118616272189452807591091542120727401415762287153293056320
|
|
)
|
|
assert count_sequences(5000)
|