## No.195 フィボナッチ数列の理解(2)

### 解法

editorialが十分分かりやすい。

fibonacci数列が$O(2^N)$で増加するというのが肝。

### 実装

• $F_{0,1}(k) = F_{1,0}(k+1)$。
• ${\mathbf A}^{-1} = \frac{1}{\det{\mathbf A}}({\rm tr}{\mathbf A}{\mathbf E} - {\mathbf A})$。
#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;
}
}