結果

問題 No.195 フィボナッチ数列の理解(2)
ユーザー mayoko_mayoko_
提出日時 2015-05-01 18:04:07
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,831 bytes
コンパイル時間 2,711 ms
コンパイル使用メモリ 161,928 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-09-19 04:54:22
合計ジャッジ時間 4,084 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i, n) for (int (i) = 0; (i) < (int)(n); (i)++)

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
typedef long long ll;

ll F1[50], F2[50];

void init() {
    F1[0] = 1, F1[1] = 0;
    for (int i = 2; i < 50; i++) {
        F1[i] = F1[i-1] + F1[i-2];
    }
    F2[0] = 0, F2[1] = 1;
    for (int i = 2; i < 50; i++) {
        F2[i] = F2[i-1] + F2[i-2];
    }
}

// 2次連立方程式の計算
// 解が存在しなかったら(-1, -1)を返す
pair<ll, ll> calc(const vector<vector<ll> >& mat) {
    ll det = mat[0][0] * mat[1][1] - mat[0][1] * mat[1][0];
    if (det == 0) {
        // 今回の問題の場合はこうなったら解は存在しない
        return make_pair(-1, -1);
    }
    pair<ll, ll> p;
    {
        ll tmp = mat[1][1]*mat[0][2] - mat[0][1] * mat[1][2];
        p.first = tmp / det;
    }
    {
        ll tmp = mat[0][0]*mat[1][2] - mat[1][0] * mat[0][2];
        p.second = tmp / det;
    }
    if (p.first*mat[0][0] + p.second*mat[0][1] != mat[0][2] || p.first*mat[1][0] + p.second*mat[1][1] != mat[1][2]) return make_pair(-1, -1);
    return p;
}

vector<pair<ll, ll> > solve(const vector<ll> X) {
    vector<pair<ll, ll> > ret;
    for (int i = 0; i < 50; i++) {
        for (int j = i+1; j < 50; j++) {
            vector<vector<ll> > mat(2, vector<ll>(3));
            mat[0][0] = F1[i];
            mat[0][1] = F2[i];
            mat[0][2] = X[0];
            mat[1][0] = F1[j];
            mat[1][1] = F2[j];
            mat[1][2] = X[1];
            auto p = calc(mat);
            if (p.first > 0 && p.second > 0) {
                ret.push_back(p);
            }
        }
    }
    sort(ret.begin(), ret.end());
    return ret;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    init();
    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());
    if (X.size() == 2) {
        auto ans = solve(X);
        cout << ans[0].first << " " << ans[0].second << endl;
    } else if (X.size() == 1) {
        X.push_back(1);
        swap(X[0], X[1]);
        auto ans = solve(X);
        cout << ans[0].first << " " << ans[0].second << endl;
    } else {
        auto Y = X;
        Y.pop_back();
        auto ans = solve(Y);
        vector<pair<ll, ll>> ansans;
        for (auto p : ans) {
            for (int i = 0; i < 50; i++) for (int j = i+1; j < 50; j++) {
                if (F1[i]*p.first + F2[i]*p.second == X[2]) ansans.push_back(p);
            }
        }
        sort(ansans.begin(), ansans.end());
        if (ansans.size() == 0) {
            cout << -1 << endl;
        } else {
            cout << ansans[0].first << " " << ansans[0].second << endl;
        }
    }
    return 0;
}
0