結果
| 問題 |
No.195 フィボナッチ数列の理解(2)
|
| コンテスト | |
| ユーザー |
maine_honzuki
|
| 提出日時 | 2020-12-16 12:06:31 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 5,000 ms |
| コード長 | 2,439 bytes |
| コンパイル時間 | 2,358 ms |
| コンパイル使用メモリ | 208,052 KB |
| 最終ジャッジ日時 | 2025-01-17 01:52:03 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 22 |
ソースコード
#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;
}
maine_honzuki