丑数

丑数是指不能被2,3,5以外的其他素数整除的数。把丑数从小到大排列起来,结果如下: 1,2,3,4,5,6,8,9,10,12,15,… 求第1500个丑数。

提示:从小到大生成各个丑数。最小的丑数是1,对于任意丑数x,2x,3x和5x也是丑数。使用一个优先队列保存已生成的丑数,每次取出最小的丑数,生成3个新的丑数。需要注意,同一个丑数有多种生成方式,所以需要判断一个丑数是否已经生成过。

题目链接:https://vjudge.net/contest/211547#problem/G

#include<iostream>
#include <functional> //包含greater
#include<vector>
#include<queue>
#include<set>
using namespace std;
typedef long long LL;
const int coeff[] = { , , }; int main() {
priority_queue<LL, vector<LL>, greater<LL> > pq;
set<LL> s;
pq.push();
s.insert();
for (int i = ; ; i++) {
LL x = pq.top(); pq.pop();
if (i == ) {
cout << "The 1500'th ugly number is " << x << ".\n";
break;
}
for (int j = ; j < ; j++) {
LL x2 = x * coeff[j];
if (!s.count(x2)) { s.insert(x2); pq.push(x2); }
}
}
return ;
}

2018-03-25  18:41:35

UVA136 Ugly Numbers【set】【优先队列】的更多相关文章

  1. UVa136 Ugly Numbers(优先队列priority_queue)

    Ugly Numbers 题目 Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, ...

  2. UVa 136 Ugly Numbers【优先队列】

    题意:给出丑数的定义,不能被除2,3,5以外的素数整除的的数称为丑数. 和杭电的那一题丑数一样--这里学的紫书上的用优先队列来做. 用已知的丑数去生成新的丑数,利用优先队列的能够每次取出当前最小的丑数 ...

  3. UVA136 Ugly Numbers

    题意 PDF 分析 用堆和集合维护即可. 时间复杂度\(O(1500 \log n)\) 代码 #include<iostream> #include<cstdio> #inc ...

  4. UVA.136 Ugly Numbers (优先队列)

    UVA.136 Ugly Numbers (优先队列) 题意分析 如果一个数字是2,3,5的倍数,那么他就叫做丑数,规定1也是丑数,现在求解第1500个丑数是多少. 既然某数字2,3,5倍均是丑数,且 ...

  5. UVA - 136 Ugly Numbers(丑数,STL优先队列+set)

    Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9 ...

  6. Ugly Numbers UVA - 136(优先队列+vector)

    Problem Description Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, ...

  7. Ugly Numbers(STL应用)

    题目链接:http://poj.org/problem?id=1338 Ugly Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

  8. 丑数(Ugly Numbers, UVa 136)

    丑数(Ugly Numbers, UVa 136) 题目描述 我们把只包含因子2.3和5的数称作丑数(Ugly Number).求按从小到大的顺序的第1500个丑数.例如6.8都是丑数,但14不是,因 ...

  9. 【UVA - 136】Ugly Numbers(set)

    Ugly Numbers Descriptions: Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequ ...

随机推荐

  1. Confluence 6 允许其他用户编辑站点欢迎消息

    你可以通过使用 Include Page 宏从你站点其他页面中包含内容,而允许其他不是 Confluence 管理员的用户编辑站点欢迎消息.使用这种方式能够避免直接对模板文件中的内容进行编辑. 从站点 ...

  2. plugin-barcodescanner 报错

    https://github.com/phonegap/phonegap-plugin-barcodescanner/issues/418 ionic cordova platform rm andr ...

  3. MySQL之IDE工具介绍及数据备份

    一.IDE工具介绍 生产环境还是推荐使用mysql命令行,但为了方便我们测试,可以使用IDE工具 下载链接:https://pan.baidu.com/s/1bpo5mqj 二.MySQL数据备份 # ...

  4. poj3254 炮兵阵地弱化版,记数类dp

    /* dp[i][j]表示到第i行的状态j有多少放置方式 */ #include<iostream> #include<cstring> #include<cstdio& ...

  5. linux下搭建SVN

      官网下载: http://subversion.apache.org/packages.html SVN客户端:TortoiseSVN :https://tortoisesvn.net/downl ...

  6. js获取url协议、url, 端口号等信息路由信息

    以路径为 http://www.baidu.com  为例 console.log("location:"+window.location.href); >> &quo ...

  7. python修改hosts

    #coding=utf-8 host = ['192.168.10.240 store.wondershare.com', '192.168.10.240 store.wondershare.jp', ...

  8. Java开发环境笔记

    在配置环境变量中 设置Java_home: 一是为了方便引用,比如,jdk安装在c:\jdk16.0目录里,则设置java_home为该目录路径,那么以后要使用这个路径的时候,只需输入%java_ho ...

  9. SQL Server表关联

    表关联:Hash.Nested Loops.Merge.这是实际算法,不是T-SQL中的inner/left/right/full/cross join.优化器会把这些T-SQL写法转换成上面的3种算 ...

  10. Ueditor设置默认字体、字号、行间距,添加字体种类(转)

    Ueditor默认字体.字号.行间距的修改: ueditor默认字号是16号,默认字体为sans-serif,默认行间距为5px,如下图所示: 首先,修改ueditor.all.js文件中如上图红框中 ...