結果

問題 No.955 ax^2+bx+c=0
ユーザー lam6er
提出日時 2025-03-31 17:22:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 110 ms / 2,000 ms
コード長 2,926 bytes
コンパイル時間 267 ms
コンパイル使用メモリ 83,136 KB
実行使用メモリ 80,760 KB
最終ジャッジ日時 2025-03-31 17:24:00
合計ジャッジ時間 15,606 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 122
権限があれば一括ダウンロードができます

ソースコード

diff #

from decimal import Decimal, getcontext

# Read input
a, b, c = map(int, input().split())

# Check if all zeros (infinite solutions)
if a == 0 and b == 0 and c == 0:
    print(-1)
else:
    if a == 0:
        # Linear case
        if b == 0:
            # c !=0, no solutions
            print(0)
        else:
            # x = -c / b
            getcontext().prec = 30
            x = Decimal(-c) / Decimal(b)
            # Formatting to avoid trailing zeros
            parts = format(x.normalize(), 'f').split('.')
            if len(parts) == 1:
                print(1)
                print(parts[0])
            else:
                integer_part, fractional_part = parts
                fractional_part = fractional_part.rstrip('0')
                if fractional_part:
                    print(1)
                    print(f"{integer_part}.{fractional_part}")
                else:
                    print(1)
                    print(integer_part)
    else:
        # Quadratic equation
        getcontext().prec = 30
        a_dec = Decimal(a)
        b_dec = Decimal(b)
        c_dec = Decimal(c)
        D = b_dec**2 - 4 * a_dec * c_dec
        
        if D < 0:
            print(0)
        elif D == 0:
            root = (-b_dec) / (2 * a_dec)
            # Formatting
            parts = format(root.normalize(), 'f').split('.')
            if len(parts) == 1:
                print(1)
                print(parts[0])
            else:
                integer_part, fractional_part = parts
                fractional_part = fractional_part.rstrip('0')
                if fractional_part:
                    print(1)
                    print(f"{integer_part}.{fractional_part}")
                else:
                    print(1)
                    print(integer_part)
        else:
            sqrt_D = D.sqrt()
            # Choose the formula based on the sign of b to avoid subtraction cancellation
            if b_dec > 0:
                numerator = -b_dec - sqrt_D
            else:
                numerator = -b_dec + sqrt_D
            root1 = numerator / (2 * a_dec)
            root2 = (c_dec / a_dec) / root1
            
            # Ensure root1 <= root2
            if root1 > root2:
                root1, root2 = root2, root1
            
            # Format the roots
            roots = []
            for root in [root1, root2]:
                parts = format(root.normalize(), 'f').split('.')
                if len(parts) == 1:
                    roots.append(parts[0])
                else:
                    integer_part, fractional_part = parts
                    fractional_part = fractional_part.rstrip('0')
                    if fractional_part:
                        roots.append(f"{integer_part}.{fractional_part}")
                    else:
                        roots.append(integer_part)
            
            print(2)
            print('\n'.join(roots))
0