Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用矩阵逆模N扩展欧几里德算法_Python_Algorithm_Matrix Inverse_Mod - Fatal编程技术网

Python 用矩阵逆模N扩展欧几里德算法

Python 用矩阵逆模N扩展欧几里德算法,python,algorithm,matrix-inverse,mod,Python,Algorithm,Matrix Inverse,Mod,我正在用矩阵modN实现一个。这是我的代码实现: def eea(a, b): if not isinstance(a, int) or not isinstance(b, int) or not a or not b: result = 'Error(eea): Invalid input num' else: original_a = a original_b = b x, y, u, v = (0, 1, 1,

我正在用矩阵mod
N
实现一个。这是我的代码实现:

def eea(a, b):
    if not isinstance(a, int) or not isinstance(b, int) or not a or not b:
        result = 'Error(eea): Invalid input num'
    else:
        original_a = a
        original_b = b
        x, y, u, v = (0, 1, 1, 0)
        while a != 0:
            q, r = (b // a, b % a)
            m, n = (x - u * q, y - v * q)
            b, a, x, y, u, v = (a, r, u, v, m, n)
        cnsm = b
        result = [cnsm, x, y]
        if original_a < 0 and original_b < 0:
            result[0] = abs(result[0])
            result[1] = -1 * result[1]
            result[2] = -1 * result[2]
        if result[0] < 0:
            result = [abs(result[0]), x, y]
            if original_a < 0 < original_b or original_b < 0 < original_a:
                result[2] = -1 * result[2]
        if result[0] > 0:
            if original_b < 0 < original_a:
                result[2] = -1 * result[2]
    return result
(这是视频链接:)


然而,我的代码只能得到
x=-11,y=-4
,确切地说,这是方程
13x=36y+1
的解,但在视频中,解是
x=25,y=9
,那么我如何更改代码以满足这种情况呢?

−11与25 mod 36是一致的,因此在Python中,您可以只取
x%N

[3, 2]
[4, 7]