2015: d25: ex1: add solution

This commit is contained in:
Bruno BELANYI 2025-05-22 04:24:04 +01:00
parent 65f558bf0e
commit 6a8aa06859

43
2015/d25/ex1/ex1.py Executable file
View file

@ -0,0 +1,43 @@
#!/usr/bin/env python
import sys
from typing import NamedTuple
class Point(NamedTuple):
x: int
y: int
def solve(input: str) -> int:
def parse(input: str) -> tuple[int, int]:
*_, row, column = input.replace(".", "").split(", ")
return int(row.split()[-1]), int(column.split()[-1])
def mod_pow(n: int, pow: int, mod: int) -> int:
if pow == 0:
return 1
if pow == 1:
return n % mod
res = mod_pow(n, pow // 2, mod) ** 2
if pow % 2 == 1:
res *= n
return res % mod
def lookup(row: int, column: int) -> int:
n = column + row - 1
top_right = n * (n + 1) // 2
i = top_right - row
return 20151125 * mod_pow(252533, i, 33554393) % 33554393
row, column = parse(input)
return lookup(row, column)
def main() -> None:
input = sys.stdin.read()
print(solve(input))
if __name__ == "__main__":
main()