313. Super Ugly Number
2016-03-08 19:17
309 查看
题目:
Write a program to find the nth super ugly number.
Super ugly numbers are positive numbers whose all prime factors are in the given prime list
size
Note:
(1)
(2) The given numbers in
(3) 0 <
106, 0 <
1000.
解题思路:
思路类似ugly number那一题
Write a program to find the nth super ugly number.
Super ugly numbers are positive numbers whose all prime factors are in the given prime list
primesof
size
k. For example,
[1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32]is the sequence of the first 12 super ugly numbers given
primes=
[2, 7, 13, 19]of size 4.
Note:
(1)
1is a super ugly number for any given
primes.
(2) The given numbers in
primesare in ascending order.
(3) 0 <
k≤ 100, 0 <
n≤
106, 0 <
primes[i]<
1000.
解题思路:
思路类似ugly number那一题
class Solution { public: int nthSuperUglyNumber(int n, vector<int>& primes) { vector<int> ugly(n,INT_MAX), index(primes.size(),0); ugly[0] = 1; for(int i=1;i<n;i++){ for(int j=0;j<primes.size();j++)ugly[i] = min(ugly[i],ugly[index[j]]*primes[j]); for(int k=0;k<primes.size();k++)index[k] += (ugly[index[k]]*primes[k]==ugly[i]); } return ugly[n-1]; } };
相关文章推荐
- Redis应用场景
- ML2分层端口绑定技术在SDN开发中的应用(一)
- 通过JVM 参数 实现spring 应用的二进制代码与配置分离。
- 学期博客:学习进度条
- 作业2
- Codeforces 630N - Forecast
- 【HDU5638 BestCoder Round 74 (div1)C】【贪心 线段树or树套树or队列】Toposort n点m边删k边使得拓扑序最小
- docker 学习笔记3
- 项目--Firebug使用小结
- cd 切换当前目录至dirName
- ABAP 通过视图取数到内表函数
- BZOJ3052: [wc2013]糖果公园
- grep正则表达式
- 让CentOS能用yum自动安装rar和unrar
- 【一起学AngularJS】第四章、Angular模版技术
- Python变量作用域
- BZOJ4016: [FJOI2014]最短路径树问题
- uva10891(sum游戏)区间dp
- 用redis实现支持优先级的消息队列
- 共享栈[数组]