usaco Chapter 3 section 3.1 Humble Numbers

来源:互联网 发布:网络构建师 编辑:程序博客网 时间:2024/05/17 03:16

 /*
ID: niepeng1
LANG: C++
TASK:humble
*/
/*
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.

*/
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#define Max1 100010
#define MaxK 110
using namespace std;

int prime[MaxK];
int pindex[MaxK]={0};
long int way[Max1];

int main()
{
 FILE *in,*out;
 in=fopen("humble.in","r");
 out=fopen("humble.out","w");
 int i,j,K,N,min,minindex;
 fscanf(in,"%d %d",&K,&N);
 for(i=0;i<K;i++)
  fscanf(in,"%d",&prime[i]);
 j=0;
 way[j]=1;
 while(j<=N)
 {
  min=0x7fffffff;
  minindex=0;
  for(i=0;i<K;i++)
  {
   while( double(way[pindex[i]])*prime[i] <= way[j])
   {
    pindex[i]++;
   }
   if(min>double(way[pindex[i]])*prime[i])
   {
    min=double(way[pindex[i]])*prime[i];
    minindex=i;
   }
  }
  pindex[minindex]++;
  way[++j]=min;
 }
 fprintf(out,"%d/n",way[N]);

 return 0;
}

原创粉丝点击