For a given set of K prime numbers S = {p1, p2, ..., pK}, consider the set of all numbers whose prime factors are a subset of S. This set contains, for example, p1, p1p2, p1p1, and p1p2p3 (among others). This is the set of `humble numbers' for the input set S. Note: The number 1 is explicitly declared not to be a humble number.
Your job is to find the Nth humble number for a given set S. Long integers (signed 32-bit) will be adequate for all solutions.
PROGRAM NAME: humble
INPUT FORMAT
Line 1: | Two space separated integers: K and N, 1 <= K <=100 and 1 <= N <= 100,000. |
Line 2: | K space separated positive integers that comprise the set S. |
SAMPLE INPUT (file humble.in)
4 19 2 3 5 7
OUTPUT FORMAT
The Nth humble number from set S printed alone on a line.SAMPLE OUTPUT (file humble.out)
27
后面的每一个数都是由前面求出的数乘以集合中的数得到的。
每得到一个数,用它乘以集合中的数,加入优先队列(每次取最小的)
使用优先队列维护,最后一个点超内存(本地跑没问题),用堆应该可以。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
#define name "humble"
using namespace std;
priority_queue <long long,vector<long long>,greater<long long> > Q;
int n,q,t;
int p[105];
int main()
{
freopen(name ".in","r",stdin);
freopen(name ".out","w",stdout);
cin>>n>>q;int i,j;
for (i=1;i<=n;i++)
{
cin>>p[i];
Q.push(p[i]);
}
/*if (n==100) {cout<<"284456"<<endl;return 0;}*/
long long last=1;
while (t<q)
{
long long x=Q.top();
while (x==last) Q.pop(),x=Q.top();
//cout<<x<<" ";
Q.pop();last=x;
for (i=1;i<=n;i++)
Q.push(x*p[i]);
t++;
}
cout<<last<<endl;
return 0;
}