結果

問題 No.320 眠れない夜に
ユーザー srup٩(๑`н´๑)۶srup٩(๑`н´๑)۶
提出日時 2016-10-23 19:08:27
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,822 bytes
コンパイル時間 640 ms
コンパイル使用メモリ 81,412 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-15 18:03:49
合計ジャッジ時間 2,251 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 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 2 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,384 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <tuple>
#include <vector>
#include <cstdio>
#include <map>
#include <algorithm>
using namespace std;
typedef long long ll;
const int INF = 1e9;
#define rep(i,n) for(int i=0;i<(n);i++)

// のこり n 回 普通のフィボナッチ数列をやった場合いくつになるかを求める
ll upper(ll a, ll b, ll n){
	vector<ll> dp(n + 10);
	dp[0] = a, dp[1] = b;
	for (int i = 2; i < n + 2; ++i)
	{
		dp[i] = dp[i - 1] + dp[i - 2];
	}
	return dp[n + 1];
}
// のこり n 回 毎回 -1 するフィボナッチ数列をやった場合いくつになるかを求める
ll lower(ll a, ll b, ll n){
	vector<ll> dp(n + 10);
	dp[0] = a, dp[1] = b;
	for (int i = 2; i < n + 2; ++i)
	{
		dp[i] = dp[i - 1] + dp[i - 2] - 1;
	}
	return dp[n + 1];
}

map<tuple<ll, ll, int, ll>, int> memo;

// fib3 = fib2(b) + fib1(a) n:残りの回数 m:目指す値
int branch_and_bound(ll a, ll b, int n, ll m){
	auto key = make_tuple(a, b, n, m);
	if(memo.count(key)) return memo[key];

	if(n == 0){
		if(b == m){//正解
			return memo[key] = 0;//間違えたの 0回
		}else{
			return memo[key] = INF;
		}
	}

	// どんなに間違えなくても、mに達しない (上界)
	if(upper(a, b, n) < m){
		return memo[key] = INF;
	}
	//どんなに間違えても、mより小さくできない (下界)
	if(lower(a, b, n) > m){
		return memo[key] = INF;
	}

	//普通のフィボナッチ数列
	int ret1 = branch_and_bound(b, a + b, n - 1, m);
	//-1したフィボナッチ数列
	int ret2 = branch_and_bound(b, a + b - 1, n - 1, m) + 1;
	//間違える回数が少ないほうを選択
	return memo[key] = min(ret1, ret2);

}

int main(void){
	int n;
	ll m;
	cin >> n >> m;
	int ret = branch_and_bound(1, 1, n - 2, m);
	if(ret == INF){
		printf("-1\n");
	}else{
		printf("%d\n", ret);
	}
	return 0;
}
0