第几个幸运数
前言
优先队列做法
一、题目
到x星球旅行的游客都被发给一个整数,作为游客编号。
x星的国王有个怪癖,他只喜欢数字3,5和7。
国王规定,游客的编号如果只含有因子:3,5,7,就可以获得一份奖品。
我们来看前10个幸运数字是:
3 5 7 9 15 21 25 27 35 45
因而第11个幸运数字是:49
小明领到了一个幸运数字 59084709587505,他去领奖的时候,人家要求他准确地说出这是第几个幸运数字,否则领不到奖品。
请你帮小明计算一下,59084709587505是第几个幸运数字。
二、代码
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef long long LL;
typedef pair<LL, int> PII;
const int N = 1e5 + 10;
priority_queue<PII, vector<PII>, greater<PII> > heap;
int main()
{
heap.push({ 3, 3 });
heap.push({ 5, 5 });
heap.push({ 7, 7 });
LL res = 59084709587505LL;
int cnt = 1;
for (int i = 1; ; i++) {
auto x = heap.top();
if (x.first == res) {
cout << cnt << endl;
break;
}
if (x.second == 3) {
heap.push({ x.first * 3, 3 });
heap.push({ x.first * 5, 5 });
heap.push({ x.first * 7, 7 });
heap.pop();
cnt++;
}
else if (x.second == 5) {
heap.push({ x.first * 5, 5 });
heap.push({ x.first * 7, 7 });
heap.pop();
cnt++;
}
else {
heap.push({ x.first * 7, 7 });
heap.pop();
cnt++;
}
}
system("pause");
return 0;
}