问题描述
小蓝定义了一个 Fibonacci 集合 F,集合的元素如下定义:
- 最小的 5 个 Fibonacci 数 1, 2, 3, 5, 8 属于集合 F。
- 如果一个元素 x 属于 F,则 3x + 2、5x + 3 和 8x + 5 都属于集合 F。
- 其他元素都不属于 F。
请问,这个集合中的第 2020 小元素的值是多少?
答案提交
这是一道结果填空的题,你只需要算出结果后提交即可。
本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。
答案:41269
题解
优先队列:
#include <iostream>
#include <unordered_set>
#include <queue>
using namespace std;
typedef long long LL;
int main()
{
unordered_set<LL> S;
priority_queue<LL, vector<LL>, greater<LL>> q;
q.push(1); S.insert(1);
q.push(2); S.insert(2);
q.push(3); S.insert(3);
q.push(5); S.insert(5);
q.push(8); S.insert(8);
int cnt = 0;
while(q.size())
{
cnt ++;
LL x = q.top(); q.pop();
if(cnt == 2020)
{
cout << x << endl;
break;
}
LL w[] = {3 * x + 2, 5 * x + 3, 8 * x + 5};
for (int i = 0; i < 3; i ++)
if(!S.count(w[i]))
{
S.insert(w[i]);
q.push(w[i]);
}
}
return 0;
}