Skip to content

Instantly share code, notes, and snippets.

@joonas-yoon
Created July 6, 2020 07:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joonas-yoon/d24b403707791f3533f46097e0142932 to your computer and use it in GitHub Desktop.
Save joonas-yoon/d24b403707791f3533f46097e0142932 to your computer and use it in GitHub Desktop.
UVa 136, POJ 1338 - Ugly Numbers
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
typedef long long lld;
struct node {
node(lld d, int p) : data(d), prev(p) {}
lld data;
int prev;
bool operator < (const node& p) const {
return data > p.data;
}
};
int main() {
priority_queue<node> pq;
pq.push(node(1, 0));
int mul[] = { 2, 3, 5 };
vector<lld> v;
while (v.size() < 1500) {
node c = pq.top();
pq.pop();
v.push_back(c.data);
for (int i = c.prev; i < 3; ++i) {
lld x = c.data * mul[i];
pq.push(node(x, i));
}
}
for (int n; ~scanf("%d", &n) && n;) {
printf("%lld\n", v[n - 1]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment