結果

問題 No.195 フィボナッチ数列の理解(2)
ユーザー mamekinmamekin
提出日時 2015-07-20 18:31:20
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,629 bytes
コンパイル時間 679 ms
コンパイル使用メモリ 96,180 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-22 20:38:48
合計ジャッジ時間 1,649 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

bool check(long long a, long long b, int x)
{
    while(a < x){
        a += b;
        swap(a, b);
    }
    return a == x;
}

void backInit(long long& a, long long& b)
{
    while(0 < b - a && b - a <= a){
        b -= a;
        swap(a, b);
    }
}

int main()
{
    vector<int> x(3);
    for(int i=0; i<3; ++i)
        cin >> x[i];
    sort(x.begin(), x.end());

    if(x[2] == 1){
        cout << 1 << ' ' << 1 << endl;
        return 0;
    }
    if(x[0] == x[2])
        x[0] = 1;
    else if(x[0] == x[1])
        x[1] = x[2];

    pair<long long, long long> ans(LLONG_MAX, LLONG_MAX);
    pair<long long, long long> curr(0, 1), prev(1, 0);
    for(;;){
        long long a = x[0];
        long long tmp = x[1] - curr.first * a;
        if(tmp <= 0)
            break;

        if(tmp % curr.second == 0){
            long long b = tmp / curr.second;
            if(check(a, b, x[2])){
                backInit(a, b);
                ans = min(ans, make_pair(a, b));
            }
        }

        prev.first += curr.first;
        prev.second += curr.second;
        swap(curr, prev);

    }

    if(ans.first == LLONG_MAX)
        cout << -1 << endl;
    else
        cout << ans.first << ' ' << ans.second << endl;

    return 0;
}
0