UVA136 Ugly Numbers
题意
分析
用堆和集合维护即可。
时间复杂度\(O(1500 \log n)\)
代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<ctime>
#include<cstring>
#define rg register
#define il inline
#define co const
template<class T>il T read()
{
rg T data=0;
rg int w=1;
rg char ch=getchar();
while(!isdigit(ch))
{
if(ch=='-')
w=-1;
ch=getchar();
}
while(isdigit(ch))
{
data=data*10+ch-'0';
ch=getchar();
}
return data*w;
}
template<class T>T read(T&x)
{
return x=read<T>();
}
using namespace std;
typedef long long ll;
co int coef[3]={2,3,5};
int main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
priority_queue< ll,vector <ll> ,greater <ll> > pq;
set <ll> s;
pq.push(1);
s.insert(1);
for(int i=1;;++i)
{
ll x=pq.top();
pq.pop();
if(i==1500)
{
cout<<"The 1500'th ugly number is "<<x<<".\n";
break;
}
for(int j=0;j<3;++j)
{
ll y=x*coef[j];
if(!s.count(y))
{
s.insert(y);
pq.push(y);
}
}
}
return 0;
}
UVA136 Ugly Numbers的更多相关文章
- UVa136 Ugly Numbers(优先队列priority_queue)
Ugly Numbers 题目 Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, ...
- UVA136 Ugly Numbers【set】【优先队列】
丑数 丑数是指不能被2,3,5以外的其他素数整除的数.把丑数从小到大排列起来,结果如下: 1,2,3,4,5,6,8,9,10,12,15,… 求第1500个丑数. 提示:从小到大生成各个丑数.最小的 ...
- 【UVA - 136】Ugly Numbers(set)
Ugly Numbers Descriptions: Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequ ...
- [POJ1338]Ugly Numbers
[POJ1338]Ugly Numbers 试题描述 Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequ ...
- Ugly Numbers
Ugly Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21918 Accepted: 9788 Descrip ...
- poj 1338 Ugly Numbers(丑数模拟)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063? viewmode=contents 题目链接:id=1338&q ...
- leetcode@ [263/264] Ugly Numbers & Ugly Number II
https://leetcode.com/problems/ugly-number/ Write a program to check whether a given number is an ugl ...
- Geeks Interview Question: Ugly Numbers
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence1, 2, 3, 4, 5, 6, 8, 9, ...
- 136 - Ugly Numbers
Ugly Numbers Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3 ...
随机推荐
- 函数:生成1-n的随机数组,
方法很笨,不过可行: #include <stdio.h> /** 功能:获取一个1-n的随机数数组,这些随机数都互不相同 ** 入参:n-表示最大随机数: *randArray -用于储 ...
- 《Python学习手册》(三)
string 字符串,和列表.元组,都适用序列操作. 关于python转义字符 迭代 for x in S: print(x) [c * 2 for c in S] Comparasion: > ...
- JavaWeb Listener
1. 监听器概述 1.1. 什么是监听器 做过Swing或者AWT图像界面Java程序开发的话,应该对Listener与Event非常熟悉.Swing或者AWT中通过Listener与Event来处理 ...
- quartz(5)--作业管理和存储
作业一旦被调度,调度器需要记住并且跟踪作业和它们的执行次数.如果你的作业是30分钟后或每30秒调用,这不是很有用.事实上,作业执行需要非常准确和即时调用在被调度作业上的execute()方法.Quar ...
- 关系型数据库的ACID规则
1.A (Atomicity) 原子性 原子性很容易理解,也就是说事务里的所有操作要么全部做完,要么都不做,事务成功的条件是事务里的所有操作都成功,只要有一个操作失败,整个事务就失败,需要回滚. 比如 ...
- fix LayerKit framework不能提交App Store
- 问题: - 原因 x86_64, i386是ios模拟器用的architectures.发布时,不支持这两种.但是,默认编译出来的layerkit framework支持这两种编译器 - 解决办法 ...
- springJDBC的几种方法
1.简单粗暴,直接在类中创建连接池使用 package com.xiaostudy; import org.apache.commons.dbcp.BasicDataSource; import or ...
- VLAN虚拟局域网技术(一)-计算机网络
本文主要知识来源于学校课程,部分知识来自于H3C公司教材,未经许可,禁止转载.如需转载,请联系作者并注明出处. 1. VLAN(Virtual LAN):我们称之为虚拟局域网,它的作用就是将物理上互 ...
- Linux集群的NTP服务器时间同步
我们搭建集群环境的时候,时间必须是要统一的,才能保证集群数据的一致性. 一般操作是直接使用NTP,跟默认的时间服务器同步,但是最好还是让所有节点跟集群中的某台作为时间服务器的节点同步. 步骤:(节点有 ...
- DPDK-KERNEL NIC INTERFACE(内核NIC接口)
DPDK编程指南(翻译)( 二十一) 21.内核网络接口卡接口 DPDK Kernel NIC Interface(KNI)允许用户空间应用程序访问Linux *控制面. 使用DPDK KNI的好处是 ...