From a527f65a9aab6003e6f48a9a33192f7a62fbc053 Mon Sep 17 00:00:00 2001 From: serr Date: Thu, 9 Jan 2025 02:00:19 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BF=D1=80=D0=B8=D1=87=D0=B5=D1=81=D0=B0?= =?UTF-8?q?=D0=BB=20=D1=87=D1=83=D1=82=D0=BA=D0=B0=20=D0=BA=D0=BE=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chain_fraction.py | 24 ++++++++++-------------- euclid.py | 3 +-- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/chain_fraction.py b/chain_fraction.py index 5beb5af..9a78959 100644 --- a/chain_fraction.py +++ b/chain_fraction.py @@ -23,28 +23,24 @@ def get_p_n(n, q_list): def get_x0(a, b, m): q_list = get_q_list(m, a) n = len(q_list) - 1 - p = get_p_n(n-1, q_list) - x = ((-1)**n*p*b)%m - return x, f'x={x}mod{m}' + p = get_p_n(n-1, get_q_list(m, a)) + x0 = ((-1)**n*p*b)%m + return x0 def solve(task): f = re.fullmatch(r'(\d+)x=(\d+)mod(\d+)', task) if f == None: return "Usage Example: py chain_fraction.py '7x=8mod13'" a, b, m = int(f.groups()[0]), int(f.groups()[1]), int(f.groups()[2]) - d = gcd(a, m) # Находим НОД - if d == 1: # нод = 1 => 1 решение - return get_x0(a, b, m)[1] + d = gcd(a, m) if d > 1 and b % d != 0: return 'No solutions' - else: - d = gcd(gcd(a, b), m) - a1, b1, m1 = a // d, b // d, m // d - x0 = get_x0(a1, b1, m1)[0] - s = "" - for k in range(d): - s += f'x={(x0+m1*k)%m}mod{m}\n' - return s[:-1] + a1, b1, m1 = a // d, b // d, m // d + x0 = get_x0(a1, b1, m1) + s = "" + for i in range(d): + s += f'x={(x0+m//d*i)%m}mod{m}\n' + return s[:-1] if __name__ == "__main__": if len(sys.argv) != 2: diff --git a/euclid.py b/euclid.py index a4fb877..e1933fe 100644 --- a/euclid.py +++ b/euclid.py @@ -17,8 +17,7 @@ def solve(task): if d > 1 and b % d != 0: return 'No solutions' a1, b1, m1 = a // d, b // d, m // d - u, _, _ = gcd(a1, m1) - x0 = b1*u + x0 = b1*gcd(a1, m1)[0] s = "" for i in range(0, d): s += f'x={(x0+m//d*i)%m}mod{m}\n'