1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| import sys with open('C:\\Users\\吴文之\\Desktop\\20个数据\\19.txt') as file_object: lines = file_object.readlines()
def gcd(a, b): while b != 0: a, b = b, a % b return a
def modreverse(c, d): if gcd(c, d) != 1: return None u1, u2, u3 = 1, 0, c v1, v2, v3 = 0, 1, d while v3 != 0: q = u3 // v3 v1, v2, v3, u1, u2, u3 = (u1 - q * v1), (u2 - q * v2), (u3 - q * v3), v1, v2, v3 return u1 % d
a1 = int(lines[0]) a2 = int(lines[1]) a3 = int(lines[2]) m1 = int(lines[3]) m2 = int(lines[4]) m3 = int(lines[5])
mark = True if gcd(m1, m2) != 1 | gcd(m1, m3) != 1 | gcd(m2, m3) != 1: sys.exit(-1) if gcd(m1, m2) != 1: print("m1和m2两两不互素,不符合条件") sys.exit(-1) if gcd(m1, m3) != 1: print("m1和m3两两不互素,不符合条件") sys.exit(-1) if gcd(m2, m3) != 1: print("m2和m3两两不互素,不符合条件") sys.exit(-1) if mark == True: M = m1 * m2 * m3 M1 = m2 * m3 M2 = m1 * m3 M3 = m1 * m2 M1_ = modreverse(M1, m1) print("M1的模逆为" + str(M1_)) M2_ = modreverse(M2, m2) print("M2的模逆为" + str(M2_)) M3_ = modreverse(M3, m3) print("M3的模逆为" + str(M3_))
x = (a1 * M1_ * M1 + a2 * M2_ * M2 + a3 * M3_ * M3) % M print("\nx="+str(a1 * M1_ * M1 + a2 * M2_ * M2 + a3 * M3_ * M3)+"\n(mod"+str(M)+")\n="+str(x)) else: print("x无解")
|