結果

問題 No.195 フィボナッチ数列の理解(2)
ユーザー roiti46roiti46
提出日時 2015-04-27 00:13:00
言語 Python2
(2.7.18)
結果
AC  
実行時間 31 ms / 5,000 ms
コード長 1,845 bytes
コンパイル時間 652 ms
コンパイル使用メモリ 6,772 KB
実行使用メモリ 6,076 KB
最終ジャッジ日時 2023-09-18 12:23:45
合計ジャッジ時間 2,326 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
5,984 KB
testcase_01 AC 11 ms
5,860 KB
testcase_02 AC 30 ms
5,856 KB
testcase_03 AC 11 ms
5,936 KB
testcase_04 AC 30 ms
5,872 KB
testcase_05 AC 11 ms
5,872 KB
testcase_06 AC 11 ms
6,020 KB
testcase_07 AC 11 ms
5,944 KB
testcase_08 AC 11 ms
5,948 KB
testcase_09 AC 11 ms
5,888 KB
testcase_10 AC 30 ms
5,984 KB
testcase_11 AC 30 ms
5,936 KB
testcase_12 AC 30 ms
5,872 KB
testcase_13 AC 30 ms
5,860 KB
testcase_14 AC 30 ms
5,932 KB
testcase_15 AC 31 ms
5,864 KB
testcase_16 AC 30 ms
6,072 KB
testcase_17 AC 30 ms
6,076 KB
testcase_18 AC 30 ms
5,980 KB
testcase_19 AC 30 ms
5,868 KB
testcase_20 AC 30 ms
5,936 KB
testcase_21 AC 30 ms
5,944 KB
testcase_22 AC 30 ms
5,944 KB
testcase_23 AC 31 ms
6,068 KB
testcase_24 AC 30 ms
5,872 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

eps = 1e-8
def gauss_jordan(A, b):
    n = len(A)
    B = [map(float, A[i] + [b[i]]) for i in xrange(n)]

    for i in xrange(n):
        pivot = i
        for j in xrange(i, n):
            if abs(B[j][i]) > abs(B[pivot][i]): pivot = j
        B[i], B[pivot] = B[pivot], B[i]
        
        if abs(B[i][i]) < eps: return []
        
        for j in xrange(i + 1, n + 1):
            B[i][j] /= B[i][i]
        for j in xrange(0, n):
            if i != j:
                for k in xrange(i + 1, n + 1):
                    B[j][k] -= B[j][i] * B[i][k]
        
    res = [0] * n
    for i in xrange(n):
        res[i] = B[i][n]
    return res

coeff = [1, 0, 1, 1]
while coeff[-2] <= 10 ** 10:
    coeff.append(coeff[-1] + coeff[-2])
N = len(coeff)

X, Y, Z = sorted(map(int, raw_input().split()))
uniq = len(set([X, Y, Z]))
if uniq == 1:
    A = 1
    B = 99999999
    for i in xrange(1, N - 1):
        if X - coeff[i] > 0 and (X - coeff[i]) % coeff[i + 1] == 0:
            B = min(B, (X - coeff[i]) / coeff[i + 1])
    print A, B
    exit()

if uniq == 2:
    X, Y = sorted(list(set([X, Y, Z])))
    
A = B = 1e10
for i in xrange(N - 1):
    for j in xrange(N - 1):
        if i == j: continue
        Ax = [[coeff[i], coeff[i + 1]],
              [coeff[j], coeff[j + 1]]]
        b = [X, Y]
        tmp = gauss_jordan(Ax, b)
        if len(tmp) == 0: continue
        tA, tB = tmp
        if min(tA, tB) < 1 or max(abs(tA - int(tA + eps)), abs(tB - int(tB + eps))) > eps: continue
        tA, tB = int(tA + eps), int(tB + eps)
        if uniq == 3:
            for k in xrange(N - 1):
                if coeff[k] * tA + coeff[k + 1] * tB == Z:
                    break
            else:
                continue
        if tA < A or tA == A and tB < B:
            A, B = tA, tB

if max(A, B) < 1e10:
    print A, B
else:
    print -1
0