USACO 3.1 Humble Numbers
Posted sigongzi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了USACO 3.1 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
————————————————————
所以就是一道set的简单应用
然而我的内存在最后一个测试点炸了
事实上操作内存不需要n*k然后第n次的begin,只要不断维护这个序列是n长最后把--end抛出去
然后就没有内存问题了,还会比较快
1 /* 2 ID: ivorysi 3 PROG: humble 4 LANG: C++ 5 */ 6 #include <iostream> 7 #include <cstdio> 8 #include <cstring> 9 #include <algorithm> 10 #include <queue> 11 #include <set> 12 #define siji(i,x,y) for(int i=(x);i<=(y);++i) 13 #define gongzi(j,x,y) for(int j=(x);j>=(y);--j) 14 #define xiaosiji(i,x,y) for(int i=(x);i<(y);++i) 15 #define sigongzi(j,x,y) for(int j=(x);j>(y);--j) 16 #define inf 0x7fffffff 17 #define MAXN 100005 18 #define ivorysi 19 using namespace std; 20 typedef long long ll; 21 set<int> s; 22 int n,k,pri[105]; 23 void solve() { 24 scanf("%d%d",&k,&n); 25 siji(i,1,k) {scanf("%d",&pri[i]);s.insert(pri[i]);} 26 siji(i,1,k){ 27 set<int>::iterator k=s.begin(); 28 while(1) { 29 int tm=(*k)*pri[i]; 30 if(tm<0) break; 31 if(s.size()>n) { 32 s.erase(--s.end()); 33 if(tm>(*--s.end())) 34 break; 35 } 36 s.insert(tm); 37 ++k; 38 } 39 } 40 printf("%d\n",*(--s.end())); 41 } 42 int main(int argc, char const *argv[]) 43 { 44 #ifdef ivorysi 45 freopen("humble.in","r",stdin); 46 freopen("humble.out","w",stdout); 47 #else 48 freopen("f1.in","r",stdin); 49 #endif 50 solve(); 51 }
以上是关于USACO 3.1 Humble Numbers的主要内容,如果未能解决你的问题,请参考以下文章