313 Super Ugly Number 超级丑数
编写一段程序来寻找第 n 个超级丑数。
超级丑数是指其所有质因数都在长度为k的质数列表primes中的正整数。例如,[1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32],是给定长度为 4 的质数列表primes = [2, 7, 13, 19]的前 12 个超级丑数。
注意:
(1) 1是任何给定的primes的超级丑数。
(2) 给定primes中的数字以升序排列。
(3) 0 < k ≤ 100, 0 < n ≤ 106, 0 < primes[i] < 1000 。
(4) 第n个超级丑数可以认为在32位有符整数的表示范围内。
详见:https://leetcode.com/problems/super-ugly-number/description/
C++:
class Solution {
public:
int nthSuperUglyNumber(int n, vector<int>& primes) {
vector<int> res(1,1),idx(primes.size(),0);
while(res.size()<n)
{
vector<int> tmp;
int mn=INT_MAX;
for(int i=0;i<primes.size();++i)
{
tmp.push_back(res[idx[i]]*primes[i]);
}
for(int i=0;i<primes.size();++i)
{
mn=min(mn,tmp[i]);
}
for(int i=0;i<primes.size();++i)
{
if(mn==tmp[i])
{
++idx[i];
}
}
res.push_back(mn);
}
return res.back();
}
};
参考:https://www.cnblogs.com/grandyang/p/5144918.html
313 Super Ugly Number 超级丑数的更多相关文章
- [LeetCode]313. Super Ugly Number超级丑数,丑数系列看这一道就行了
丑数系列的题看这一道就可以了 /* 和ugly number2差不多,不过这次的质因子多了,所以用数组来表示质因子的target坐标 target坐标指的是这个质因子此次要乘的前任丑数是谁 */ pu ...
- [LeetCode] 313. Super Ugly Number 超级丑陋数
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- [LeetCode] Super Ugly Number 超级丑陋数
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- [LintCode] Super Ugly Number 超级丑陋数
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes
263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...
- 313. Super Ugly Number
题目: Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose ...
- Leetcode 313. super ugly number
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- 263. Ugly Number + 264. Ugly Number II + 313. Super Ugly Number
▶ 三个与丑数相关的问题 ▶ 第 263题,判定一个数字是否是丑数,即其素因子是否仅由 2,3,5 构成. ● 常规消除判别,4 ms class Solution { public: bool is ...
- LeetCode OJ:Ugly Number(丑数)
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
随机推荐
- 洛谷 4768 LOJ 2718「NOI2018」归程
[题解] 本题有多种做法,例如可持久化并查集.kruskal重构树等. kruskal重构树的做法是这样的:先把边按照海拔h从大到小的顺序排序,然后跑kruskal建立海拔的最大生成树,顺便建krus ...
- 洛谷 2213 [USACO14MAR]懒惰的牛The Lazy Cow_Sliver
[题解] 每个格子可以到达的区域是一个菱形,但是我们并不能快速的求和,所以我们可以把原来的草地旋转45度,用二维前缀和快速处理菱形的区域的和. #include<cstdio> #incl ...
- 【01】bootstrap基本信息
[01]基本信息 中文官网:http://www.bootcss.com/ 英文官网:https://github.com/twbs/bootstrap/ 支持IE8+ CND : htt ...
- 【Codeforces 1036C】Classy Numbers
[链接] 我是链接,点我呀:) [题意] 让你求出只由3个非0数字组成的数字在[li,ri]这个区间里面有多少个. [题解] 只由3个非0数字组成的数字在1~10^18中只有60W个 dfs处理出来之 ...
- CodeForcesGym 100753F Divisions
Divisions Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. Or ...
- noip模拟赛 花
[问题描述]商店里出售n种不同品种的花.为了装饰桌面,你打算买m支花回家.你觉得放两支一样的花很难看,因此每种品种的花最多买1支.求总共有几种不同的买花的方案?答案可能很大,输出答案mod p的值. ...
- js控制frameset的rows
window.parent.document.getElementById("MainWork").rows="*,0" ;
- codevs3730 无线网络发射选址
题目描述 Description 随着智能手机的日益普及,人们对无线网的需求日益增大.某城市决定对城市内的公共场所覆盖无线网. 假设该城市的布局为由严格平行的129条东西向街道和129条南北向街道所形 ...
- 我不喜欢的 Rust 特性 (之一) eager drop
struct Foo; impl Drop for Foo { fn drop(&mut self) { println!("drop"); } } fn main() { ...
- Caused by: java.lang.ClassNotFoundException: org.springframework.boot.context.embedded.FilterRegistrationBean
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.context.embedded.FilterRegistr ...