2018-12-16 17:09:54 +08:00
|
|
|
#coding: utf-8
|
|
|
|
''' mbinary
|
|
|
|
#######################################################################
|
|
|
|
# File : gcd.py
|
|
|
|
# Author: mbinary
|
|
|
|
# Mail: zhuheqin1@gmail.com
|
2019-01-31 12:09:46 +08:00
|
|
|
# Blog: https://mbinary.xyz
|
2018-12-16 17:09:54 +08:00
|
|
|
# Github: https://github.com/mbinary
|
|
|
|
# Created Time: 2018-12-16 10:06
|
|
|
|
# Description:
|
|
|
|
#######################################################################
|
|
|
|
'''
|
|
|
|
|
2020-04-15 12:28:20 +08:00
|
|
|
|
|
|
|
def gcd(a, b):
|
|
|
|
while b != 0:
|
|
|
|
a, b = b, a % b
|
2018-12-16 17:09:54 +08:00
|
|
|
return a
|
|
|
|
|
2020-04-15 12:28:20 +08:00
|
|
|
|
|
|
|
def lcm(a, b):
|
|
|
|
return int(a*b/gcd(a, b))
|
|
|
|
|
|
|
|
|
|
|
|
def xgcd(a, b):
|
2018-12-16 17:09:54 +08:00
|
|
|
'''return gcd(a,b), x,y where ax+by=gcd(a,b)'''
|
2020-04-15 12:28:20 +08:00
|
|
|
if b == 0:
|
|
|
|
return a, 1, 0
|
|
|
|
g, x, y = xgcd(b, a % b)
|
|
|
|
return g, y, x-a//b*y
|
|
|
|
|
2018-12-16 17:09:54 +08:00
|
|
|
|
2020-04-15 12:28:20 +08:00
|
|
|
if __name__ == '__main__':
|
2018-12-16 17:09:54 +08:00
|
|
|
while 1:
|
|
|
|
a = int(input('a: '))
|
|
|
|
b = int(input('b: '))
|
2020-04-15 12:28:20 +08:00
|
|
|
print('gcd :', gcd(a, b))
|
|
|
|
print('xgcd:', xgcd(a, b))
|