Linear Equation Solver
Python
Medium
2 views
Problem Description
Read a and b for equation a*x + b = 0. Output x as reduced fraction p/q. If no solution output NONE. If infinite solutions output ALL.
Input Format
One line: a b.
Output Format
p/q or NONE or ALL.
Constraints
a,b are integers in -10^9..10^9.
Official Solution
import sys,math
s=sys.stdin.read().strip().split()
if not s: sys.exit(0)
a=int(s[0]); b=int(s[1])
if a==0:
sys.stdout.write('ALL' if b==0 else 'NONE')
else:
p=-b
q=a
g=math.gcd(abs(p),abs(q))
p//=g; q//=g
if q<0:
p=-p; q=-q
sys.stdout.write(f"{p}/{q}")
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!