算法竞赛入门经典 习题5-15

UVa12333

Revenge of Fibonacci

给定一个斐波那契数列的前缀(小于等于40),找出具有该前缀的斐波那契数的最小下标(小于100000)。

这道题输入的前缀长度可以达到40,下标的上界为100000,而斐波那契数列的增长速度很快,因此绝对会超出内置类型所能表示的范围,所以必须要使用高精度算法。

根据斐波那契数列的递推公式,大概到2000项时,斐波那契数就有约100位了,简单估计一下,100000项的斐波那契数的长度有几万位,这肯定会超时,所以还需要进行优化。

由于输入前缀长度最大只有40位,因此只需要保证前40位的正确性即可,所以可以在计算的过程中只对最高的40位有效数字进行计算。但是为了保证前40位的正确性,应该多保留一些位数。

如果高精度算法使用10^18次方进制,那么一位数字便可以表达十进制下的18个有效数字,只需保留大概4个数字即可满足题目要求,精度范围从55位到72位。

最后,前缀查找应该使用字典树(Trie)。由于题目要求输出具有该前缀的斐波那契数的最小的下标,因此在向字典树中添加前缀时,每到一个节点都应该将index更新为较小的值。

#include <iostream>
#include <algorithm>
#include <array>
#include <string>
#include <vector>

#define BASE 1000000000000000000

using namespace std;

struct TrieNode
{
	int index;
	array<TrieNode*, 10> next;

	TrieNode() : index(-1)
	{
		next.fill(nullptr);
	}

	void add(const string &s, int value)
	{
		TrieNode* ptr = this;
		for (char c : s)
		{
			if (ptr->next[c - '0'] == nullptr) {
				ptr->next[c - '0'] = new TrieNode();
			}
			ptr = ptr->next[c - '0'];
			if (ptr->index == -1) {
				ptr->index = value;
			}
			else {
				ptr->index = min(ptr->index, value);
			}
		}
	}

	int search(const string &prefix)
	{
		TrieNode* ptr = this;
		for (char c : prefix)
		{
			if (ptr->next[c - '0'] == nullptr) {
				return -1;
			}
			ptr = ptr->next[c - '0'];
		}
		return ptr->index;
	}
};

string Convert2String(const vector<long long> &fib)
{
	string s, tmp;
	s.append(to_string(fib.back()));
	for (auto riter = fib.rbegin() + 1; riter != fib.rend(); riter++)
	{
		tmp = to_string(*riter);
		s.append(18 - tmp.length(), '0');
		s.append(tmp);
	}
	return s;
}

vector<long long> PrecomputeFib(const vector<long long> &fn_2, vector<long long> &fn_1)
{
	long long carry = 0;
	size_t i = 0, j = 0;
	vector<long long> result;
	while (i < fn_2.size() && j < fn_1.size()) {
		result.push_back(fn_2[i] + fn_1[j] + carry);
		carry = result.back() / BASE;
		result.back() %= BASE;
		i++, j++;
	}
	while (j < fn_1.size()) {
		result.push_back(fn_1[j] + carry);
		carry = result.back() / BASE;
		result.back() %= BASE;
		j++;
	}
	if (carry != 0) {
		result.push_back(carry);
	}
	if (result.size() > 4) {
		fn_1.assign(fn_1.begin() + (result.size() - 4), fn_1.end());
		result.assign(result.begin() + result.size() - 4, result.end());
	}
	return result;
}

void PrecomputeFibPrefix(TrieNode &root)
{
	vector<long long> fn_2(1, 1), fn_1(1, 1), fn;
	int index = 2;
	root.add("1", 0);
	while (index < 100000) {
		fn = PrecomputeFib(fn_2, fn_1);
		root.add(Convert2String(fn), index);
		fn_2 = fn_1;
		fn_1 = fn;
		index++;
	}
}

int main()
{
	int n = 0;
	cin >> n;
	TrieNode root;
	PrecomputeFibPrefix(root);
	for (int i = 0; i < n; i++)
	{
		string prefix;
		cin >> prefix;
		cout << "Case #" << i + 1 << ": " << root.search(prefix) << endl;
	}
	return 0;
}
/*
15
1
12
123
1234
12345
9
98
987
9876
98765
89
32
51075176167176176176
347746739
5610
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值