結果

問題 No.195 フィボナッチ数列の理解(2)
ユーザー 古寺いろは古寺いろは
提出日時 2015-04-26 23:57:11
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 485 ms / 5,000 ms
コード長 3,803 bytes
コンパイル時間 1,407 ms
コンパイル使用メモリ 167,912 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-05 03:04:15
合計ジャッジ時間 8,311 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 97 ms
6,816 KB
testcase_01 AC 485 ms
6,944 KB
testcase_02 AC 98 ms
6,940 KB
testcase_03 AC 98 ms
6,940 KB
testcase_04 AC 98 ms
6,940 KB
testcase_05 AC 98 ms
6,944 KB
testcase_06 AC 99 ms
6,940 KB
testcase_07 AC 368 ms
6,940 KB
testcase_08 AC 387 ms
6,940 KB
testcase_09 AC 130 ms
6,944 KB
testcase_10 AC 119 ms
6,940 KB
testcase_11 AC 340 ms
6,940 KB
testcase_12 AC 99 ms
6,940 KB
testcase_13 AC 278 ms
6,940 KB
testcase_14 AC 194 ms
6,940 KB
testcase_15 AC 401 ms
6,940 KB
testcase_16 AC 146 ms
6,940 KB
testcase_17 AC 170 ms
6,940 KB
testcase_18 AC 278 ms
6,944 KB
testcase_19 AC 181 ms
6,940 KB
testcase_20 AC 373 ms
6,940 KB
testcase_21 AC 374 ms
6,940 KB
testcase_22 AC 397 ms
6,944 KB
testcase_23 AC 283 ms
6,940 KB
testcase_24 AC 399 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define T 50

#define MAX 1000000001

long long A[T];
long long B[T];


long long ansA = MAX;
long long ansB = MAX;

int main() {
	A[0] = 1;
	B[1] = 1;
	for (int i = 2; i < T; i++)
	{
		A[i] = A[i - 1] + A[i - 2];
		B[i] = B[i - 1] + B[i - 2];
	}

	vector<long long> num(3);
	cin >> num[0] >> num[1] >> num[2];
	sort(num.begin(), num.end());

	if (num[0] == num[1] && num[1] == num[2]){
		ansA = 1;
		ansB = num[0];
		if (num[0] != 1) ansB--;
	}
	if (num[0] == num[1] || num[1] == num[2]){
		int tempA = num[0];
		int tempB = num[2];
		if (tempA > tempB) swap(tempA, tempB);
		if (ansA > tempA || (ansA == tempA && ansB > tempB)){
			ansA = tempA;
			ansB = tempB;
		}
	}

	if (num[0] + num[1] == num[2]){
		int tempA = num[0];
		int tempB = num[1];
		if (tempA > tempB) swap(tempA, tempB);
		if (ansA > tempA || (ansA == tempA && ansB > tempB)){
			ansA = tempA;
			ansB = tempB;
		}
	}
	
	for (int i = 0; i < T; i++)
	{
		if (A[i] + B[i] > num[0]) continue;
		for (int j = 0; j < T; j++)
		{
			if (A[j] + B[j] > num[1]) continue;
			for (int k = 0; k < T; k++)
			{
				if (A[k] + B[k] > num[2]) continue;
				if (A[k] == 0 || B[k] == 0) continue;

				double lowa = 1;
				double higha = num[2] / A[k] + 1;


				if (lowa > higha) continue;
				for (int l = 0; l < 100; l++)
				{
					double a1 = (lowa + lowa + higha) / 3;
					double a2 = (lowa + higha + higha) / 3;

					double b1 = (num[2] - a1 * A[k]) / B[k];
					double b2 = (num[2] - a2 * A[k]) / B[k];

					double diff1 = 0;
					double diff2 = 0;

					diff1 += abs(num[0] - (a1 * A[i] + b1 * B[i]));
					diff1 += abs(num[1] - (a1 * A[j] + b1 * B[j]));

					diff2 += abs(num[0] - (a2 * A[i] + b2 * B[i]));
					diff2 += abs(num[1] - (a2 * A[j] + b2 * B[j]));

					if (diff1 < diff2) higha = a2;
					else lowa = a1;
				}
				int ta = (int)(lowa + 0.5);
				for (int a = max(ta - 100, 1); a <= ta + 100; a++){
					int b = (num[2] - a * A[k]) / B[k];
					if (a <= 0) continue;
					if (b <= 0) continue;
					if (num[0] != a * A[i] + b * B[i]) continue;
					if (num[1] != a * A[j] + b * B[j]) continue;
					if (num[2] != a * A[k] + b * B[k]) continue;

					if (ansA > a || (ansA == a && ansB > b)){
						ansA = a;
						ansB = b;
					}
				}
			}
		}
	}

	for (int a = 1; a <= 50000; a++)
	{
		for (int e = 2; e < T; e++)
		{
			int b = (num[2] - a * A[e]) / B[e];
			if (b <= 0) continue;
			if (a * A[e] + b * B[e] != num[2]) continue;
			int ok = 0;
			for (int c = 0; c < 2; c++)
			{
				bool flag = false;
				for (int d = 0; d < T; d++)
				{
					if (num[c] == a * A[d] + b * B[d]){
						flag = true;
						break;
					}
				}
				if (flag) ok++;
			}
			if (ok == 2){
				if (ansA > a || (ansA == a && ansB > b)){
					ansA = a;
					ansB = b;
				}
			}
		}
	}

	for (int b = 1; b <= 50000; b++)
	{
		for (int e = 2; e < T; e++)
		{
			int a = (num[2] - b * B[e]) / A[e];
			if (a <= 0) continue;
			if (a * A[e] + b * B[e] != num[2]) continue;
			int ok = 0;
			for (int c = 0; c < 2; c++)
			{
				bool flag = false;
				for (int d = 0; d < T; d++)
				{
					if (num[c] == a * A[d] + b * B[d]){
						flag = true;
						break;
					}
				}
				if (flag) ok++;
			}
			if (ok == 2){
				if (ansA > a || (ansA == a && ansB > b)){
					ansA = a;
					ansB = b;
				}
			}
		}
	}

	for (int a = 1; a <= 500; a++)
	{
		for (int b = 1; b <= 500; b++)
		{
			int ok = 0;
			for (int c = 0; c < 3; c++)
			{
				bool flag = false;
				for (int d = 0; d < T; d++)
				{
					if (num[c] == a * A[d] + b * B[d]){
						flag = true;
						break;
					}
				}
				if (flag) ok++;
			}
			if (ok == 3){
				if (ansA > a || (ansA == a && ansB > b)){
					ansA = a;
					ansB = b;
				}
			}
		}
	}

	if (ansA == MAX) cout << -1 << endl;
	else cout << ansA << " " <<  ansB << endl;
}
0