Save Princess(丑数)

Save Princess

时间限制:1000 ms  |  内存限制:65535 KB

难度:2

描述

Yesterday, the princess was kidnapped by a devil. The prince has to rescue our pretty princess.

"OK, if you want to save the beautiful princess, you must answer my questions correctly."the devil says.

"No problem!".

"I’ll ask you t questions. For each question, I’ll tell you an integer n, you must tell me the i th beatuiful number. If your answer is wrong, the princess and you will all die".

"But what is the characteristic of the beautiful number?" Pince asks.

"Beautiful numbers are numbers whose only prime factors are 2, 3 or 5. The sequence

1, 2, 3, 4, 5, 6, 8, 9, 10, ...   shows the first 9 beautiful numbers.

By convention, 1 is included. "

Can you help the prince to save the princess?

输入
The input for each case is an integer n(1≤n≤5000) and it is terminated by a negative integer.
输出
For each test case, you should print an integer which represents the i th beautiful number.
样例输入
2
3
-1
样例输出
2
3

题解:这个方法解决丑数非常实用;

代码:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<vector>
 7 #define mem(x,y) memset(x,y,sizeof(x))
 8 using namespace std;
 9 const int INF=0x3f3f3f3f;
10 const int MAXN=5010;
11 typedef long long LL;
12 LL ans[MAXN];
13 LL MIN(LL x,LL y,LL z){
14     LL d=x;
15     if(y<d)d=y;
16     if(z<d)d=z;
17     return d;
18 }
19 int main(){
20     int n,x_2=1,x_3=1,x_5=1;
21     LL d;
22     ans[1]=1;
23     int i=1;
24     while(i<=5000){
25         i++;
26         d=MIN(ans[x_2]*2,ans[x_3]*3,ans[x_5]*5);
27         ans[i]=d;
28         if(d==ans[x_2]*2)x_2++;
29         if(d==ans[x_3]*3)x_3++;
30         if(d==ans[x_5]*5)x_5++;
31     }
32     while(scanf("%d",&n),n>0)printf("%lld\n",ans[n]);
33     return 0;
34 }
时间: 2024-08-06 03:40:55

Save Princess(丑数)的相关文章

nyoj1032——Save Princess——————【set应用】

Save Princess 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 Yesterday, the princess was kidnapped by a devil. The prince has to rescue our pretty princess. "OK, if you want to save the beautiful princess, you must answer my questions correctly."the dev

Humble Numbers(丑数) 超详解!

给定一个素数集合 S = { p[1],p[2],...,p[k] },大于 1 且素因子都属于 S 的数我们成为丑数(Humble Numbers or Ugly Numbers),记第 n 大的丑数为 h[n]. 算法 1: 一种最容易想到的方法当然就是从 2 开始一个一个的判断一个数是否为丑数.这种方法的复杂度约为 O( k * h[n]),铁定超时(如果你这样做而没有超时,请跟 tenshi 联系) 算法 2: 看来只有一个一个地主动生成丑数了 : 我最早做这题的时候,用的是一种比较烂的

洛谷P2723 丑数 Humble Numbers [2017年 6月计划 数论07]

P2723 丑数 Humble Numbers 题目背景 对于一给定的素数集合 S = {p1, p2, ..., pK},考虑一个正整数集合,该集合中任一元素的质因数全部属于S.这个正整数集合包括,p1.p1*p2.p1*p1.p1*p2*p3...(还有其 它).该集合被称为S集合的“丑数集合”.注意:我们认为1不是一个丑数. 题目描述 你的工作是对于输入的集合S去寻找“丑数集合”中的第N个“丑数”.所有答案可以用longint(32位整数)存储. 补充:丑数集合中每个数从小到大排列,每个丑

插入排序的优化【不靠谱地讲可以优化到O(nlogn)】 USACO 丑数

首先我们先介绍一下普通的插排,就是我们现在一般写的那种,效率是O(n^2)的. 普通的插排基于的思想就是找位置,然后插入进去,其他在它后面的元素全部后移,下面是普通插排的代码: 1 #include<iostream> 2 #include<fstream> 3 #include<stdio.h> 4 using namespace std; 5 int a[200000]; 6 int p[200000]; 7 8 int main(){ 9 ios::sync_wi

lintcode 中等题:Ugly Numbers 丑数

题目 丑数 设计一个算法,找出只含素因子3,5,7 的第 k 大的数. 符合条件的数如:3,5,7,9,15...... 样例 如果k=4, 返回 9 挑战 要求时间复杂度为O(nlogn)或者O(n) 解题 法一:直接暴力,逐次判断一个数是不是丑数 下面只对其中的奇数判断是否是丑数,加不加奇数都超时. class Solution { /** * @param k: The number k. * @return: The kth prime number as description. */

34 - 丑数

题目描述:http://ac.jobdu.com/problem.php?pid=1214 把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N个丑数. 解析: 丑数的定义应该为:质因子只含有2.3.5的数 .1 默认是一个丑数. e.g. 8 = 2*2*2; 18 = 2*3*3 都是丑数 直观想法是: 判断一个数是否是丑数,如果它能被2整除,则一直除以2,同理能被3,5整除

HDU 丑数 - 1058 Humble Numbers

这一题是讲了一个名叫丑数的概念(为啥叫丑数,,). 概念:因子中仅仅包含2.3.5,7的数,称为丑数.但其实我百度网上时,发现正常的丑数应该是因子中仅仅包含2.3.5,不过基本都一样. 我们可以通过不断mod2,3,5,7,直到无法在摸,验证此时是否为1来判断该数是否为丑数,但是这样的方法太过浪费时间,所以介绍一种新的方法,下面方法摘自这里点击打开链接. 根据丑数的定义,丑数应该是另一个丑数乘以2.3,5或者7的结果(1除外).因此我们可以创建一个数组,里面的数字是排好序的丑数.里面的每一个丑数

丑数 uva-136 丑数

代码如下: /*丑数是指不能被2.3.5以外的其他素数整除的数,把丑数从小到大排列起来,结果如下: 1,2,3,4,5,6,8,9,10,12,15... 求出第1500个丑数. */ #include<iostream> #include<vector> #include<queue> #include<set> using namespace std; typedef long long LL; int su[3]={2,3,5}; int main()

NYOJ1097 Ugly Numbers 【丑数】

Ugly Numbers 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ... shows the first 11 ugly numbers. By convention, 1 is included. Now give you need