結果

問題 No.195 フィボナッチ数列の理解(2)
ユーザー PachicobuePachicobue
提出日時 2017-10-11 16:29:28
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 3,979 bytes
コンパイル時間 2,695 ms
コンパイル使用メモリ 173,572 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-10 23:06:44
合計ジャッジ時間 2,740 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

#define show(x) cerr << #x << " = " << x << endl

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz=" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}

template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p)
{
    os << "(" << p.first << "," << p.second
       << ")";
    return os;
}


constexpr ll MOD = 1e9 + 7;

template <typename T>
constexpr T INF = numeric_limits<T>::max() / 100;
constexpr ll MAX = (ll)1e9;

using P = pair<ll, ll>;
bool solve(const P& c1, const P& c2, const ll X, const ll Y, P& ans)
{
    const ll XX = c2.second * X - c1.second * Y;
    const ll YY = -c2.first * X + c1.first * Y;
    const ll Z = c1.first * c2.second - c1.second * c2.first;
    if (Z == 0 or XX % Z != 0 or YY % Z != 0) {
        return false;
    } else {
        const ll XXX = XX / Z;
        const ll YYY = YY / Z;
        if (XXX > 0 and YYY > 0) {
            ans = make_pair(XXX, YYY);
            return true;
        } else {
            return false;
        }
    }
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    vector<P> coeff;
    pair<ll, ll> a = make_pair(1, 0);
    coeff.push_back(a);
    pair<ll, ll> b = make_pair(0, 1);
    while (b.first + b.second < MAX) {
        coeff.push_back(b);
        const auto b_ = make_pair(a.first + b.first, a.second + b.second);
        a = b;
        b = b_;
    }
    const int size = coeff.size();

    const P INFP = make_pair(INF<ll>, INF<ll>);

    ll X, Y, Z;
    cin >> X >> Y >> Z;
    ll tmp[3] = {X, Y, Z};
    sort(tmp, tmp + 3);
    X = tmp[0];
    Y = tmp[1];
    Z = tmp[2];
    if (X != Y and Y != Z) {
        P mini = INFP;
        for (int i = 0; i < size - 2; i++) {
            for (int j = i + 1; j < size - 1; j++) {
                P p;
                if (solve(coeff[i], coeff[j], X, Y, p)) {
                    for (int k = j + 1; k < size; k++) {
                        if (p.first * coeff[k].first + p.second * coeff[k].second == Z) {
                            mini = min(mini, p);
                        }
                    }
                }
            }
        }
        if (mini != INFP) {
            cout << mini.first << " " << mini.second << endl;
        } else {
            cout << -1 << endl;
        }
    } else if (X != Y) {
        P mini = INFP;
        for (int i = 0; i < size - 1; i++) {
            for (int j = i + 1; j < size; j++) {
                P p;
                if (solve(coeff[i], coeff[j], X, Y, p)) {
                    mini = min(mini, p);
                }
            }
        }
        if (mini != INFP) {
            cout << mini.first << " " << mini.second << endl;
        } else {
            cout << -1 << endl;
        }
    } else if (Y != Z) {
        P mini = INFP;
        for (int i = 0; i < size - 1; i++) {
            for (int j = i + 1; j < size; j++) {
                P p;
                if (solve(coeff[i], coeff[j], Y, Z, p)) {
                    mini = min(mini, p);
                }
            }
        }
        if (mini != INFP) {
            cout << mini.first << " " << mini.second << endl;
        } else {
            cout << -1 << endl;
        }
    } else {
        if (Z > 1) {
            bool flag = false;
            for (int i = size - 1; i >= 0; i--) {
                if (coeff[i].first + coeff[i].second <= Z and (Z - coeff[i].first) % coeff[i].second == 0) {
                    cout << 1 << " " << (Z - coeff[i].first) / coeff[i].second << endl;
                    flag = true;
                    break;
                }
            }
            if (not flag) {
                cout << 1 << " " << Z - 1 << endl;
            }
        } else {
            cout << "1 1" << endl;
        }
    }

    return 0;
}
0