def pgcd(a,b):
    if b==0:
        return a
    else:
        r=a%b
        return abs(pgcd(b,r))
def truncate(num,n=0):
    integer=int(num*(10**n))/(10**n)
    return int(float(integer))
def to_str(nombre):
    if nombre==0:
        nombre=str('')
    return nombre
def moins(chaine):  # © KikooDX
    def full_rep(chaine,before,after):
        while before in chaine:
            chaine=chaine.replace(before,after)
        return chaine
    to_replace=(
        ("+-", "-"),
        (" 1x", " x"),
        (" 1y", " y"),
        ("+1x", "+x"),
        ("+1y", "+y"),
        ("-1x", "-x"),
        ("-1y", "-y"),
        ("(+y", "(y"),
        ("(+x", "(x"),
        ("(y)", "y"),
        ("(x)", "x"),
        ("- -", "+"),
        ("--", "+"),
        ("++", "+"),
    )
    for pair in to_replace:
        chaine=full_rep(chaine,pair[0],pair[1])
    return chaine
Z=0
print("Donnez vos A, B, C tels que Ax+By=C")
A=int(input("A = "))
B=int(input("B = "))
C=int(input("C = "))
D=pgcd(A,B)
if A==0 and B==0 and C==0:
    print("L'equation est verifiee")
    exit()
if C%D!=0:
    print("Pas de solution pour cette equation")
    exit()
elif A==0:
    print("\nLa solution est triviale :")
    C=int(C/B)
    STR20="y = "+str(C)+" , x est libre"
    print(moins(STR20))
elif B==0:
    print("\nLa solution est triviale :")
    C=int(C/A)
    STR19="x = "+str(C)+" , y est libre"
    print(moins(STR19))
elif B!=0:
    STR1="On cherche (x,y) tel que "+str(A)+"x+"+str(B)+"y="+str(C)
    print(moins(STR1), "\n")
    print("Soit D = PGCD(", A, ",", B, ") = ", D, sep="")
    STR2=str(A)+"x+"+str(B)+"y="+str(C)+" est equivalent a :"
    print(moins(STR2))
    A=int(A/D)
    B=int(B/D)
    C=int(C/D)
    STR3=str(A)+"x+"+str(B)+"y="+str(C)+" (on divise par D)"
    print(moins(STR3),"\n")
    while truncate(((1-A*Z)/B))!=((1-A*Z)/B):
        Z+=1
    X=int(C*Z)
    Y=int(C*(1-A*Z)/B)
    W=int(input("Entrer une solution particuliere ?\n[1]Oui / [2]Non : "))
    if W==1:
        X=int(input("X_0 = "))
        Y=int(input("Y_0 = "))
    print("")
    print("La solution particuliere est :")
    print("X_0 =",X)
    print("Y_0 =",Y, "\n")
    STR4=str(A)+"x+"+str(B)+"y = "+str(C)+" est equivalent a"
    print(moins(STR4))
    STR5=str(A)+"x+"+str(B)+"y = "+str(A)+"*"+str(X)+" + "+str(B)+"*"+str(Y)
    print(moins(STR5))
    D=pgcd(A,B)
    A=to_str(A)
    B=to_str(B)
    X=to_str(X)
    Y=to_str(-Y)
    STR6="<=>  "+str(A)+"("+str(X)+"-x) = "+str(B)+"("+str(Y)+"+y)"
    print(moins(STR6), "\n")
    STR7="Donc "+str(A)+"|"+str(B)+"("+str(Y)+"+y)"
    print(moins(STR7))
    STR8="Or PGCD("+str(A)+","+str(B)+")="+str(D)
    print(moins(STR8))
    STR9="D'ou "+str(A)+" divise "+str(Y)+"+y, d'apres le Theoreme de GAUSS."
    print(moins(STR9), "\n")
    STR10="Autrement dit : "+str(Y)+"+y="+str(A)+"k"
    print(moins(STR10), "\n")
    STR11=str(A)+"("+str(X)+"-x) = "+str(B)+"("+str(Y)+"+y)"
    print(moins(STR11))
    STR12="("+str(Y)+"+y) = "+str(A)+"k"
    print(moins(STR12), "\n")
    STR13=str(A)+"("+str(X)+"-x) = "+str(B)+"*"+str(A)+"k"
    print(moins(STR13))
    STR14="y = "+str(A)+"*"+"k - "+str(Y)
    print(moins(STR14), "\n")
    STR15=str(X)+"-x = "+str(B)+"*"+"k"
    print(moins(STR15))
    STR16="y = "+str(A)+"*"+"k - "+str(Y)
    print(moins(STR16), "\n")
    print("Les solutions sont donc :")
    STR17="x = "+str(X)+" - "+str(B)+"*"+"k"
    print(moins(STR17))
    STR18="y = "+str(A)+"*"+"k - "+str(Y)
    print(moins(STR18))