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 ...
随机推荐
- 我们的动机(Our motivation)
我们的动机(Our motivation) There are many PHP frameworks nowadays, but none of them is like Phalcon (Real ...
- Hybrid框架UI重构之路:四、分而治之
上文回顾:Hybird框架UI重构之路:三.工欲善其事,必先利其器 上一篇文章有说到less.grunt这两个工具,是为了css.js分模块使用的.UI框架提供给使用者的时候,是一个大的xxx.js. ...
- 学习zepto.js(Hello World)
Zepto是一个轻量级的针对现代高级浏览器的JavaScript库, 它与jquery有着类似的api. 如果你会用jquery,那么你也会用zepto. 昨天听说了zepto.js,正好最近也比较闲 ...
- 开发https应用
开发https应用 SSL, 或者Secure Socket Layer,是一种允许web浏览器和web服务器通过一个安全的连接进行交流的技术.这意味着将被发送的数据在一端被翻译成密码,传送出去,然后 ...
- 浅谈GridLayout(网格布局)
Android 4.0 布局-->GridLayout 网格布局 以行列单元格的形式展示内部控件排列,可以实现类似计算机键盘效果 ,也可以实现可自动变行的标签群效果 使用GridLayout , ...
- 【代码笔记】iOS-清除缓存有黑色背景(仿环球时报)
一,效果图. 二,代码. -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UIAlertView * alterVi ...
- NSoperation用法详解及与GCD的比较
NSInvocationOperation // 基于一个对象和selector来创建操作.如果你已经有现有的方法来执行需要的任务,就可以使用这个类 NSInvocationOperation * o ...
- iOS PushMeBaby日志提示SSLwrite():-36 94
在测试远程推送时用PushMeBaby来模拟服务器发推送消息 但是点击push按钮后手机没有收到推送消息,多点击几下后程序就崩了,强行运行后日志输出了 SSLwrite():-36 94这句话 出现这 ...
- java jdbc 连接mysql数据库 实现增删改查
好久没有写博文了,写个简单的东西热热身,分享给大家. jdbc相信大家都不陌生,只要是个搞java的,最初接触j2ee的时候都是要学习这么个东西的,谁叫程序得和数据库打交道呢!而jdbc就是和数据库打 ...
- php多文件上传数组 转换
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><meta ...