結果

問題 No.195 フィボナッチ数列の理解(2)
ユーザー anta
提出日時 2015-11-14 06:35:08
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,029 bytes
コンパイル時間 612 ms
コンパイル使用メモリ 69,040 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-13 16:41:09
合計ジャッジ時間 1,639 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
using namespace std;
typedef long long ll;
const ll INFL = 1LL<<62;

int main() {
	int X[3];
	cin >> X[0] >> X[1] >> X[2];
	sort(X, X + 3);
	if(X[0] == X[2])
		X[0] = 1;
	vector<ll> f = {1, 0};
	while(f.back() < X[2])
		f.push_back(f.end()[-1] + f.end()[-2]);
	int n = f.size() - 1;
	pair<ll, ll> ans(INFL,INFL);
	vector<ll> v;
	rep(i, n) rep(j, n) if(i != j) {
		ll det = f[i] * f[j+1] - f[i+1] * f[j];
		if(det == 0) continue;
		ll xnum = X[0] * f[j+1] - X[2] * f[i+1];
		ll ynum = X[2] * f[i] - X[0] * f[j];
		if(xnum % det != 0 || ynum % det != 0) continue;
		ll x = xnum / det, y = ynum / det;
		if(x <= 0 || y <= 0) continue;
		v = { x, y };
		while(v.back() < X[1])
			v.push_back(v.end()[-1] + v.end()[-2]);
		if(find(v.begin(), v.end(), X[1]) != v.end())
			ans = min(ans, make_pair(x, y));
	}
	if(ans.first == INFL)
		cout << "-1" << endl;
	else
		cout << ans.first << " " << ans.second << endl;
	return 0;
}
0