USACO3.1Humble Numbers[...]
题目背景
对于一给定的素数集合 S = {p1, p2, ..., pK},考虑一个正整数集合,该集合中任一元素的质因数全部属于S。这个正整数集合包括,p1、p1*p2、p1*p1、p1*p2*p3...(还有其它)。该集合被称为S集合的“丑数集合”。注意:我们认为1不是一个丑数。
题目描述
你的工作是对于输入的集合S去寻找“丑数集合”中的第N个“丑数”。所有答案可以用longint(32位整数)存储。
补充:丑数集合中每个数从小到大排列,每个丑数都是素数集合中的数的乘积,第N个“丑数”就是在能由素数集合中的数相乘得来的(包括它本身)第n小的数。
输入输出格式
输入格式:
第 1 行: 二个被空格分开的整数:K 和 N , 1<= K<=100 , 1<= N<=100,000.
第 2 行: K 个被空格分开的整数:集合S的元素
输出格式:
单独的一行,输出对于输入的S的第N个丑数。
输入输出样例
4 19
2 3 5 7
27
---------------------------------------------------------------------------------------------------------------
分析:对于一个丑数,很明显是比它小的丑数*S中一个数
法一:最小堆
先认为1是丑数,推入1;
弹出堆顶tmp,推入tmp*S中每个元素【判重判重判重】
n+1次弹出就是ans
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
typedef long long ll;
const ll N=1e5+,K=;
ll n,k,a[K],ans[N],cnt=;
priority_queue<ll,vector<ll>,greater<ll> > q;
int main(){
cin>>k>>n;
for(ll i=;i<=k;i++){scanf("%d",&a[i]);}
q.push();
while(cnt<=n){
ll tmp=q.top();q.pop();
if(ans[cnt]<tmp){ //repeat
ans[++cnt]=tmp;
for(int i=;i<=k;i++)q.push(a[i]*tmp);
}
}
cout<<ans[n+];
}
然而会TLE一个点
法二:
对于ans[i],是a[j]*ans[t]:t<i中大于ans[i-1]且最小的一个
维护一个s[j]是a[j]*ans[s[j]]>f[i-1]最小的t
然后就很愉快了
#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
const ll N=1e5+,K=,INF=1e10;
ll n,k,ans[N],a[K],s[N];
int main(){
cin>>k>>n;
for(ll i=;i<=k;i++) scanf("%d",&a[i]); ans[]=;
for(ll i=;i<=n;i++){
ll now=INF;
for(ll j=;j<=k;j++){
while(a[j]*ans[s[j]]<=ans[i-]) s[j]++;//维护s[j]
now=min(now,a[j]*ans[s[j]]);//更新f[i]
}
ans[i]=now;
}
cout<<ans[n];
}
USACO3.1Humble Numbers[...]的更多相关文章
- Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range
在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字
Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Bitwise AND of Numbers Range 数字范围位相与
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- [LeetCode] Valid Phone Numbers 验证电话号码
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...
- [LeetCode] Consecutive Numbers 连续的数字
Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...
- [LeetCode] Compare Version Numbers 版本比较
Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...
随机推荐
- nodejs--实现跨域抓取数据
最近公司安排给我一个任务,抓取页面数据:http://survey.finance.sina.com.cn/static/20205/20131120.html?pid=20205&dpc=1 ...
- Web前端开发工具总结
前端开发工具: web前端开发乃及其它的相关开发, 推荐sublime text, webstorm(jetbrains公司系列产品)这两个的原因在于,有个技术叫emmet, http://docs. ...
- AloneJs.msgbox() —— 弹出消息框
一.引用 <link href="https://cdn.suziyun.com/alonejs.min.css" rel="stylesheet" /& ...
- SAP中禁止特定用户更改密码
在SAP管理中,有时一些账号因为是提供给大家作查询用的,受密码强度策略限制,密码不能为空.故密码设为通用后在公司内发布,为避免有些用户更改后造成其他用户无法登陆,我们可在使用TC-SU01,在登录数据 ...
- sharepoint项目遇到的WebDAV和HTTP PUT的安全隐患解决办法
最近一个项目,客户进行了安全检测,检测出如下安全隐患,其实这些隐患全是IIS设置的事情 许多人误认为SharePoint是在使用由IIS提供的WebDAV功能. 实际上, SharePoint在S ...
- Android 4.4沉浸式状态栏的实现
要实现Android 4.4上面的沉浸式状态栏要用到开源项目SystemBarTint(https://github.com/hexiaochun/SystemBarTint) public clas ...
- 你真的了解UIApplication吗?
一:首先查看一下关于UIApplication的定义 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIApplication : UIResponder //获得单例 ...
- iOS 教你如何实现手势密码
本次讲的手势密码,是在九个按键上实现的,这里讲的是手势密码的基本实现和效果 同样先上效果图 其实就是对画图功能的一个实现,再加上手势操作结合起来 屏幕宽度高度,方便下面操作,不做解释 #define ...
- 转:SVN常见问题与解决方法
今天发现一个SVN很奇葩的问题.原来SVN提交的时候也是识别提交路径的大小写的... 发现网上有篇博客总结的挺好的.转载下来,转载出路:http://blog.csdn.net/shinn613/ar ...
- C#语言基础——结构体和枚举类型
结构体和枚举类型 一.结构体(struct) 结构类型是用户自己定义的一种类型,它是由其他类型组合而成的,可包含构造函数.常数.字段.方法.属性.索引器.运算符.事件和嵌套类型的值类型.结构在几个重要 ...