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. Hibernate学习实现查询

  2. f-measure[转]

    F-Measure又称为F-Score,是IP(信息检索)领域常用的一个评价标准,计算公式为: 其中β是参数,P是准确率(Precision),R是召回率(Recall). F1-Measure:当参 ...

  3. C++面向对象编程,继承,数据抽象,动态绑定

    派生类(derived class)能够继承基类(base class )定义的成员: 1).派生类可以无需改变而使用那些与派生类具体特性不相关的操作 2).可以重新定义那些与派生类相关的成员函数,将 ...

  4. 【刷题】洛谷 P2764 最小路径覆盖问题

    题目描述 «问题描述: 给定有向图G=(V,E).设P 是G 的一个简单路(顶点不相交)的集合.如果V 中每个顶点恰好在P 的一条路上,则称P是G 的一个路径覆盖.P 中路径可以从V 的任何一个顶点开 ...

  5. 如何提升集群资源利用率? 阿里容器调度系统Sigma 深入解析

    阿里妹导读:为了保证系统的在线交易服务顺利运转,最初几年,阿里都是在双11大促来临之前大量采购机器储备计算资源,导致了双11之后资源大量闲置点现象.是否能把计算任务与在线服务进行混合部署,在现有弹性资 ...

  6. 【基础】ASP.net MVC 文件下载的几种方法(欢迎讨论)

    在ASP.net MVC 中有几种下载文件的方法 前提:要下载的文件必须是在服务器目录中的,至于不在web项目server目录中的文件下载我不知道,但是还挺想了解的. 第一种:最简单的超链接方法,&l ...

  7. 洛谷 P2757 [国家集训队]等差子序列 解题报告

    P2757 [国家集训队]等差子序列 题目描述 给一个\(1\)到\(N\)的排列\(\{A_i\}\),询问是否存在 \[1 \le p_1<p_2<p_3<p_4<p_5& ...

  8. 20165218 2017-2018-1 《Java程序设计》第一周学习总结

    20165218 2017-2018-1 <Java程序设计>第一周学习总结 教材学习内容总结 第一章.Java入门 1. Java特点 Java具有简单.面向对象.平台无关.多线程.动态 ...

  9. HDOJ(HDU).4508 湫湫系列故事――减肥记I (DP 完全背包)

    HDOJ(HDU).4508 湫湫系列故事――减肥记I (DP 完全背包) 题意分析 裸完全背包 代码总览 #include <iostream> #include <cstdio& ...

  10. 51nod 1486 大大走格子(DP+组合数学)

    枚举不合法点的思想. 把障碍x坐标为第一关键字,y坐标为第二关键字排序.f[i]表示走到第i个障碍的方案数. f[i]=C(x[i]+y[i]-2,x[i]-1)-sigma(f[j]*C(x[i]- ...