用一个数组p来维护每个素数要拓展的丑数在数组中的位置,每次比较得到最小的数字加入到丑数数组中。
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
#include<iostream>
using namespace std;
typedef long long LL;
/*
ID: kkkwjx1
PROG: humble
LANG: C++
*/
int s[105];
int p[105];
int v[100100];
int main()
{
freopen("humble.in","r",stdin);
freopen("humble.out","w",stdout);
int k,n;
scanf("%d%d",&k,&n);
for(int i=0; i<k; ++i)
scanf("%d",&s[i]);
int cnt=0,now=0;
v[0]=1;
while(true)
{
for(int i=0; i<k; ++i)
if(v[p[i]]*s[i]<v[p[now]]*s[now])
now=i;
v[++cnt]=v[p[now]]*s[now];
if(cnt==n)
break;
for(int i=0; i<k; ++i)
while(v[p[i]]*s[i]<=v[cnt])
++p[i];
}
printf("%d\n",v[n]);
return 0;
}