結果

問題 No.195 フィボナッチ数列の理解(2)
ユーザー maine_honzukimaine_honzuki
提出日時 2020-12-16 12:06:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 2,439 bytes
コンパイル時間 2,662 ms
コンパイル使用メモリ 217,168 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-20 09:19:41
合計ジャッジ時間 4,161 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

vector<pair<ll, ll>> find(vector<ll> X) {
    assert((int)X.size() == 2);

    vector<ll> Fib1(2), Fib2(2);
    Fib1[0] = 1;
    for (int i = 2;; i++) {
        ll x = Fib1[i - 2] + Fib1[i - 1];
        if (x < 2e9)
            Fib1.emplace_back(x);
        else
            break;
    }
    Fib2[1] = 1;
    for (int i = 2;; i++) {
        ll x = Fib2[i - 2] + Fib2[i - 1];
        if (x < 2e9)
            Fib2.emplace_back(x);
        else
            break;
    }
    int N = Fib1.size() - 1;

    vector<pair<ll, ll>> ret;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            //Fib1[i]  Fib2[i]
            //Fib1[j]  Fib2[j]
            ll det = Fib1[i] * Fib2[j] - Fib2[i] * Fib1[j];
            if (det == 0)
                continue;
            //inv
            //Fib2[j]/det    -Fib2[i]/det
            //-Fib1[j]/det Fib1[i]/det
            ll A = Fib2[j] * X[0] - Fib2[i] * X[1], B = -Fib1[j] * X[0] + Fib1[i] * X[1];
            if (A % det == 0 && B % det == 0) {
                A /= det;
                B /= det;
                if (A > 0 && B > 0) {
                    ret.push_back({A, B});
                }
            }
        }
    }
    return ret;
}

int main() {
    vector<ll> X(3);
    for (int i = 0; i < 3; i++) {
        cin >> X[i];
    }
    sort(X.begin(), X.end());
    X.erase(unique(X.begin(), X.end()), X.end());

    pair<ll, ll> ans;
    if (X.size() == 1 || X.size() == 2) {
        if (X.size() == 1)
            X.insert(X.begin(), 1);
        auto V = find(X);
        sort(V.begin(), V.end());
        ans = V[0];
    } else if (X.size() == 3) {
        vector<ll> Y(2);
        Y[0] = X[0];
        Y[1] = X[1];
        auto V = find(Y);

        auto check = [](pair<ll, ll> p, ll C) {
            ll A = p.first, B = p.second;
            if (A == C || B == C)
                return true;
            while (B < 2e9) {
                A += B;
                swap(A, B);
                if (B == C)
                    return true;
            }
            return false;
        };

        ans = {2e9, 2e9};
        for (auto p : V)
            if (check(p, X[2]))
                if (p < ans)
                    ans = p;

        if (ans.first == (ll)2e9) {
            cout << -1 << endl;
            return 0;
        }
    }
    cout << ans.first << " " << ans.second << endl;
}
0