USACO 3.1.3 HUMBLE NUMBERS

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


这道题我进行了2个优化,其实有一个是抄别人的。。。因为第一个优化在第六个点光荣的TLE了
第一个优化:对于所要求的S数组的每一个值设立一个值now,记录下一个在质数数组中要乘以的值


第二个优化:我对于这个真的无语了。。
为什么我不能用质数数组来弄呢。。。对于质数数组设立一个now,记录下一个在s数组中要乘以的值。这下秒杀这道题了

有大牛说可以用优先队列,但我觉得不行,因为从第一个扩展的所有点不见得都是最小的

   Test 1: TEST OK [0.000 secs, 3600 KB]
  
Test 2: TEST OK [0.000 secs, 3600 KB]
  
Test 3: TEST OK [0.000 secs, 3600 KB]
  
Test 4: TEST OK [0.011 secs, 3600 KB]

   Test 5: TEST OK [0.011 secs, 3600 KB]

   Test 6: TEST OK [0.043 secs, 3600 KB]

   Test 7: TEST OK [0.011 secs, 3600 KB]

   Test 8: TEST OK [0.011 secs, 3600 KB]

   Test 9: TEST OK [0.000 secs, 3600 KB]

   Test 10: TEST OK [0.011 secs, 3600 KB]

   Test 11: TEST OK [0.000 secs, 3600 KB]

   Test 12: TEST OK [0.086 secs, 3600 KB]
View Code
/*
ID:kaisada2
PROG:humble
LANG:C++
*/
#include<iostream>
#include<string.h>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>

using namespace std;

int k,n;
struct crf{
       int x;
       int index;
}a[101];

int s[100001];

int find(int x)
{
   int min1=2147483647;
   for(int i=1;i<=k;i++)
   {
      if(a[i].x*s[a[i].index]<min1)
      {
         min1=a[i].x*s[a[i].index];
      }
   } 
   for(int i=1;i<=k;i++)
   {
      if(a[i].x*s[a[i].index]==min1)
      {
         a[i].index++;
      }
   }
   return min1;
}


int main( )
{
    freopen("humble.in","r",stdin);
    freopen("humble.out","w",stdout);
    cin>>k>>n;
    for(int i=1;i<=k;i++)
    {
       cin>>a[i].x;
       a[i].index=0;
    }
    s[0]=1;
    for(int i=1;i<=n;i++)
    {
       s[i]=find(i);
    }
    cout<<s[n]<<endl;
    return 0;
}

 

posted @ 2012-08-04 09:18  kaisadadi  阅读(190)  评论(0编辑  收藏  举报