結果

問題 No.195 フィボナッチ数列の理解(2)
ユーザー kimiyukikimiyuki
提出日時 2016-02-29 01:11:56
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,805 bytes
コンパイル時間 626 ms
コンパイル使用メモリ 69,232 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 18:58:05
合計ジャッジ時間 1,439 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
#include <cassert>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
typedef long long ll;
template <typename T> bool setmin(T & l, T const & r) { if (not (r < l)) return false; l = r; return true; }
using namespace std;
int main() {
    vector<ll> xs(3); cin >> xs[0] >> xs[1] >> xs[2];
    sort(xs.begin(), xs.end());
    xs.erase(unique(xs.begin(), xs.end()), xs.end());
    const int l = 48;
    vector<ll> fib(l); fib[0] = 1; repeat (i,fib.size()-2) fib[i+2] = fib[i] + fib[i+1];
    assert (xs.back() < fib.back());
    if (xs.size() == 1) {
        xs.push_back(1);
        swap(xs[0], xs[1]);
    }
    ll x = xs[0], y = xs[1];
    pair<ll,ll> ans = { LLONG_MAX, -1 };
    repeat (i,l-1) {
        repeat (j,l-1) {
            ll det = fib[i] * fib[j+1] - fib[i+1] * fib[j];
            if (det == 0) continue;
            ll a =   fib[j+1] * x - fib[i+1] * y;
            ll b = - fib[j]   * x + fib[i]   * y;
            if (a % det != 0 or b % det != 0) continue;
            a /= det; b /= det;
            if (a <= 0 or b <= 0) continue;
            assert (a * fib[i] + b * fib[i+1] == x);
            assert (a * fib[j] + b * fib[j+1] == y);
            if (xs.size() == 3) {
                ll z = xs[2];
                bool found = false;
                repeat (k,l-1) {
                    if (a * fib[k] + b * fib[k+1] == z) {
                        found = true;
                        break;
                    }
                }
                if (not found) continue;
            }
            setmin(ans, make_pair(a, b));
        }
    }
    if (ans.first == LLONG_MAX) {
        cout << -1 << endl;
    } else {
        cout << ans.first << ' ' << ans.second << endl;
    }
    return 0;
}
0