Humble Numbers

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
思路:暴力吧,但是我只能说我STL写的太臭了!!!看了7个半小时别人的代码才勉强通过。
Executing...
   Test 1: TEST OK [0.000 secs, 3360 KB]
   Test 2: TEST OK [0.000 secs, 3360 KB]
   Test 3: TEST OK [0.000 secs, 3360 KB]
   Test 4: TEST OK [0.011 secs, 3492 KB]
   Test 5: TEST OK [0.022 secs, 3888 KB]
   Test 6: TEST OK [0.065 secs, 5604 KB]
   Test 7: TEST OK [0.022 secs, 4020 KB]
   Test 8: TEST OK [0.022 secs, 4020 KB]
   Test 9: TEST OK [0.000 secs, 3360 KB]
   Test 10: TEST OK [0.000 secs, 3360 KB]
   Test 11: TEST OK [0.000 secs, 3360 KB]
   Test 12: TEST OK [0.281 secs, 5604 KB]

All tests OK.
/*
ID:wuhuaju2
PROG:humble
LANG:C++
*/

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <set>
using namespace std;


int a[110];
int n,p,l;

set<int> s;

void close()
{
	fclose(stdin);
	fclose(stdout);
	exit(0);
}

void print(set<int> s)
{
	set<int>::iterator it;
	for (it=s.begin();it!=s.end();it++)
	cout<<*it<<" ";
	cout<<endl;
}


void work()
{
}

void init ()
{
freopen("humble.in","r",stdin);
freopen("humble.out","w",stdout);
   scanf("%d %d",&n,&p);
	for (int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		s.insert(a[i]);
	}
	for (int i=1;i<=n;i++)
	{
		set<int>::iterator it=s.begin();
		while (2)
		{
			int t=(*it)*a[i];
			if (t<0) break;
	//		cout<<"t:"<<t<<endl;
			l=s.size();
			if (l>p)
			{
				s.erase(--s.end());
				if (t>*(--s.end()))
					break;
			}
	//		print(s);
			s.insert(t);
			it++;
		}
	}
	cout<<*--s.end()<<endl;
}

int main ()
{
	init();
	work();
	close();
	return 0;
}

 

posted on 2013-02-06 19:42  cssystem  阅读(315)  评论(0编辑  收藏  举报