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

题意:对于一给定的素数集合 S = {p1, p2, ..., pK},考虑一个正整数集合,该集合中任一元素的质因数全部属于S。这个正整数集合包括,p1、p1*p2、p1*p1、p1*p2*p3...(还有其它)。该集合被称为S集合的“丑数集合”。

注意:我们认为1不是一个丑数。

你的工作是对于输入的集合S去寻找“丑数集合”中的第N个“丑数”。所有答案可以用longint(32位整数)存储。

补充:丑数集合中每个数从小到大排列,每个丑数都是素数集合中的数的乘积,第N个“丑数”就是在能由素数集合中的数相乘得来的(包括它本身)第n小的数。

/*
ID: LinKArftc
PROG: humble
LANG: C++
*/ #include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-8
#define randin srand((unsigned int)time(NULL))
#define input freopen("input.txt","r",stdin)
#define debug(s) cout << "s = " << s << endl;
#define outstars cout << "*************" << endl;
const double PI = acos(-1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
typedef long long ll; const int maxn = ;
const int maxm = ; ll num[maxm], ri[maxm], ans[maxn];
int k, n; int main() {
freopen("humble.in", "r", stdin);
freopen("humble.out", "w", stdout);
int tot = ;
scanf("%d %d", &n, &k);
for (int i = ; i < n; i ++) scanf("%lld", &num[i]);
ans[tot ++] = ;
memset(ri, , sizeof(ri));
while (tot < k + ) {
int ii;
ll mi = 0x7fffffffffffffff;
for (int i = ; i < n; i ++) {
while (num[i] * ans[ri[i]] <= ans[tot-]) ri[i]++;
if (num[i] * ans[ri[i]] < mi) {
mi = num[i] * ans[ri[i]];
ii = i;
}
}
ans[tot++] = mi;
ri[ii] ++;
}
printf("%lld\n", ans[k]); return ;
}

humble_USACO的更多相关文章

随机推荐

  1. bpf程序

    bpf都是怎么起作用的? 记得bpf之前是绑定在bpf bpf作用在哪里呀?

  2. BZOJ 1036 树的统计(树链剖分)

    点权树链剖分模板题. # include <cstdio> # include <cstring> # include <cstdlib> # include &l ...

  3. 【bzoj1593】[Usaco2008 Feb]Hotel 旅馆 线段树区间合并

    题目描述 奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光.作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿.这个巨大的旅馆一共有N (1 <= N & ...

  4. [洛谷P1714]切蛋糕

    题目大意:给你n个整数,求出其中长度不超过m的最大字段和. 题解:单调队列维护前缀和最小值,然后用当前值减去当前有效最小值即可 C++ Code: #include<cstdio> usi ...

  5. bzoj 3217: ALOEXT

    将此神题作为博客园的第一篇文章,至此,数据结构基本学完了(或者说数据结构轮流虐了我一次!) 人生第一道7K代码题! 没什么,就是treap套个trie,然后tle是因为一定要用指针当时p党谁会用那么丑 ...

  6. 【BZOJ4520】K远点对(KD-Tree)

    [BZOJ4520]K远点对(KD-Tree) 题面 BZOJ 洛谷 题解 考虑暴力. 维护一个大小为\(K\)的小根堆,然后每次把两个点之间的距离插进去,然后弹出堆顶 这样子可以用\(KD-Tree ...

  7. 【CSU1911】Card Game(FWT)

    [CSU1911]Card Game(FWT) 题面 vjudge 题目大意: 给定两个含有\(n\)个数的数组 每次询问一个数\(x\),回答在每个数组中各选一个数,或起来之后的结果恰好为\(x\) ...

  8. BZOJ2938 & 洛谷2444:[POI2000]病毒——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=2938 https://www.luogu.org/problemnew/show/P2444 二进制 ...

  9. bzoj1778: [Usaco2010 Hol]Dotp 驱逐猪猡(概率DP+高斯消元)

    深夜肝题...有害身心健康QAQ 设f[i]为到达i的概率,d[i]为i的度数. 因为无限久之后炸弹爆炸的概率是1,所以最后在i点爆炸的概率实际上就是f[i]/sigma(f[]) 列出方程组 f[i ...

  10. Nginx 配置解析

    概述:本篇文章主要对Nginx配置文件中一些常用配置进行了讲解,和如何使用Docker进行安装Nginx.因为该文章是回首在工作闲暇之余整理的,还有待完善,如果有疑义和更好的建议的朋友可以留言给我. ...