結果

問題 No.195 フィボナッチ数列の理解(2)
ユーザー t98slidert98slider
提出日時 2022-11-13 19:38:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,236 bytes
コンパイル時間 1,793 ms
コンパイル使用メモリ 175,104 KB
実行使用メモリ 4,376 KB
最終ジャッジ日時 2023-10-13 03:47:55
合計ジャッジ時間 3,340 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

// a * x + b * y = v0
// c * x + d * y = v1
pair<ll,ll> niziho(ll a, ll b, ll v0, ll c, ll d, ll v1){
    ll div = a * d - b * c;
    if(div == 0)return make_pair(-1, -1);
    ll num1 = v0 * d - b * v1;
    ll num2 = a * v1 - c * v0;
    if(num1 % div != 0 || num2 % div != 0)return make_pair(-1, -1);
    return make_pair(num1 / div, num2 / div);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    vector<ll> a(3);
    cin >> a[0] >> a[1] >> a[2];
    vector<pair<ll,ll>> b(46);
    sort(a.begin(), a.end());
    a.erase(unique(a.begin(), a.end()), a.end());
    b[0] = {1, 0};
    b[1] = {0, 1};
    for(int i = 2; i < 46; i++){
        b[i] = {b[i - 1].first + b[i - 2].first, b[i - 1].second + b[i - 2].second};
    }
    pair<ll,ll> ans = {1ll << 60, 1ll << 60};
    if(a.size() == 3){
        for(int i = 2; i < 46; i++){
            for(int j = 4; j < 46; j++){
                auto p = niziho(b[i].first, b[i].second, a[0], b[j].first, b[j].second, a[1]);
                if(p.first <= 0 || p.second <= 0)continue;
                for(int k = j; k < 46; k++){
                    ll v = p.first * b[k].first + p.second * b[k].second;
                    if(v > a[2])break;
                    if(v == a[2]){
                        ans = min(ans, p);
                        break;
                    }
                }
            }
        }
    }else if(a.size() == 2){
        ans = {a[0], a[1]};
        for(int i = 2; i < 46; i++){
            for(int j = 4; j < 46; j++){
                auto p = niziho(b[i].first, b[i].second, a[0], b[j].first, b[j].second, a[1]);
                if(p.first <= 0 || p.second <= 0)continue;
                ans = min(ans, p);
            }
        }
    }else if(a.size() == 1){
        ans = {a[0], a[0]};
        for(int i = 1; i < 46; i++){
            ll v = a[0] - b[i].first;
            if(v <= 0)break;
            if(v % b[i].second == 0){
                v /= b[i].second;
                ans = min(ans, make_pair(1ll, v));
            }
        }
    }
    if(ans.first >> 60 & 1){
        cout << -1 << '\n';
        return 0;
    }
    cout << ans.first << " " << ans.second << '\n';
}
0