結果

問題 No.195 フィボナッチ数列の理解(2)
ユーザー pekempeypekempey
提出日時 2015-10-08 09:25:24
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 2,243 bytes
コンパイル時間 1,115 ms
コンパイル使用メモリ 152,520 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-27 07:57:38
合計ジャッジ時間 2,360 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define GET_MACRO(a, b, c, NAME, ...) NAME
#define rep(...) GET_MACRO(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define rep2(i, a) rep3 (i, 0, a)
#define rep3(i, a, b) for (int i = (a); i < (b); i++)
#define repr(...) GET_MACRO(__VA_ARGS__, repr3, repr2)(__VA_ARGS__)
#define repr2(i, a) repr3 (i, 0, a)
#define repr3(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define chmin(a, b) ((b) < a && (a = (b), true))
#define chmax(a, b) (a < (b) && (a = (b), true))
using namespace std;
typedef long long ll;

ll C[50][2][2];

ll det(ll a, ll b, ll c, ll d) {
	return a * d - b * c;
}

int main() {
	ll X, Y, Z;
	cin >> X >> Y >> Z;
	vector<ll> v = {X, Y, Z};
	sort(v.begin(), v.end());
	v.erase(unique(v.begin(), v.end()), v.end());
	X = v[0];
	if (v.size() >= 2) Y = v[1];
	if (v.size() >= 3) Z = v[2];
	C[0][0][0] = 1;
	C[0][0][1] = 0;
	C[0][1][0] = 0;
	C[0][1][1] = 1;
	C[1][0][0] = 0;
	C[1][0][1] = 1;
	C[1][1][0] = 1;
	C[1][1][1] = 1;
	rep (l, 2, 50) {
		rep (i, 2) rep (j, 2) rep (k, 2) {
			C[l][i][j] += C[l - 1][i][k] * C[1][k][j];
		}
	}
	const ll inf = 1e18;
	typedef pair<ll, ll> P;
	P ans(inf, inf);
	if (v.size() == 1) {
		rep (i, 50) {
			ll a = C[i][0][0], b = C[i][0][1];
			if (b == 0) continue;
			if ((X - a) % b != 0) continue;
			ll B = (X - a) / b;
			if (B <= 0) continue;
			chmin(ans, P(1, B));
		}
	} else if (v.size() == 2) {
		rep (i, 50) rep (j, 50) {
			ll D = det(C[i][0][0], C[i][0][1], C[j][0][0], C[j][0][1]);
			ll A = det(X, C[i][0][1], Y, C[j][0][1]);
			ll B = det(C[i][0][0], X, C[j][0][0], Y);
			if (D == 0) continue;
			if (A % D != 0 || B % D != 0) continue;
			A /= D;
			B /= D;
			if (A <= 0 || B <= 0) continue;
			chmin(ans, P(A, B));
		}
	} else {
		rep (i, 50) rep (j, 50) rep (k, 50) {
			ll D = det(C[i][0][0], C[i][0][1], C[j][0][0], C[j][0][1]);
			ll A = det(X, C[i][0][1], Y, C[j][0][1]);
			ll B = det(C[i][0][0], X, C[j][0][0], Y);
			if (D == 0) continue;
			if (A % D != 0 || B % D != 0) continue;
			A /= D;
			B /= D;
			if (A <= 0 || B <= 0) continue;
			if (Z != C[k][0][0] * A + C[k][0][1] * B) continue;
			chmin(ans, P(A, B));
		}
	}
	if (ans.first == inf) {
		cout << -1 << endl;
	} else {
		cout << ans.first << " " << ans.second << endl;
	}
	return 0;
}
0