HDU 1058||Uva 136 丑数

来源:互联网 发布:centos kde gnome共存 编辑:程序博客网 时间:2024/05/16 05:57

HDU 1058

Humble Numbers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22972    Accepted Submission(s): 10053


Problem Description
A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, ... shows the first 20 humble numbers.

Write a program to find and print the nth element in this sequence
 

Input
The input consists of one or more test cases. Each test case consists of one integer n with 1 <= n <= 5842. Input is terminated by a value of zero (0) for n.
 

Output
For each test case, print one line saying "The nth humble number is number.". Depending on the value of n, the correct suffix "st", "nd", "rd", or "th" for the ordinal number nth has to be used like it is shown in the sample output.
 

Sample Input
1234111213212223100100058420
 

Sample Output
The 1st humble number is 1.The 2nd humble number is 2.The 3rd humble number is 3.The 4th humble number is 4.The 11th humble number is 12.The 12th humble number is 14.The 13th humble number is 15.The 21st humble number is 28.The 22nd humble number is 30.The 23rd humble number is 32.The 100th humble number is 450.The 1000th humble number is 385875.The 5842nd humble number is 2000000000.
 

Source
University of Ulm Local Contest 1996
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:  1003 1159 1069 1087 1421 

丑数问题,有各种思路,先从最简单的看起
丑数的定义:度娘
本题中的丑数,是不能被2,3,5,7以外的其他素数整除的数。1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27,.......是最前面的20个丑数。
1.简单粗暴的打表,先打出素数表,再从1循环到2e9,删去能被除2,3,5,7以外的素数整除的数,保存符合条件的数。BUT,问题是,1~2e9?打表?逗吧!方法一GG。
2.找规律(由于语言表述不好在此引用学长博客)
源地址:http://blog.csdn.net/lvshubao1314/article/details/18843759
首先,判断一个数是否为丑数的方法如下:
int find_uglynum(int a){    while(a%2==0)//将这个数中的质因子 2 耗尽       a/=2;    while(a%3==0)        a/=3;    while(a%5==0)       a/=5;    while(a/7==0)       a/=7;    if(a==1)       return 1;    else        return 0;}
但是这种方法过于费时,下面给出该题解题思路:
         首先,第一个丑数为“1”,后面的每一个丑数都是有前一个丑数乘2、3、5或7而来,那么后一个丑数就是前一个乘这四个数得到的最小值,for example:第一个:1,第二个:1*2、1*3、1*5或1*7,显然为2,第三个:2*2,1*3,1*5或1*7,显然是3,第四个:2*2,,2*3,1*5,1*7为4,第五个:3*2,2*3,1*5,1*7……   聪明的你是否看明白了呢?
 
下面给出本题的代码:
#include <iostream>  #include <cstdio>  using namespace std;  #define min(a,b) ((a)<(b)?(a):(b))  #define min4(a,b,c,d) min(min(a,b),min(c,d))  int a[5850];  int main()  {      //freopen("data.in","r",stdin);      //freopen("data.out","w",stdout);        int n=1;        int p2,p3,p5,p7;      p2=p3=p5=p7=1;      a[1]=1;      while(n<5843)//枚举5842个丑数,放在数组a里。      {          a[++n]=min4(2*a[p2],3*a[p3],5*a[p5],7*a[p7]);//从现在枚举的4个丑数里,先选择小的放在a里。          if(a[n]==2*a[p2])p2++;//如果a[n]==2*a[p2],2*a[p2]可能是吧a[n]枚举出的数,这样p2++,也可能是重复的枚举,这样也是p2++,总之p2++。          if(a[n]==3*a[p3])p3++;//同理。          if(a[n]==5*a[p5])p5++;//同理。          if(a[n]==7*a[p7])p7++;//同理。      }      while(scanf("%d",&n)&&n)      {          printf("%d\n",a[n]);//要谁找谁。      }      return 0;  }  


刘汝佳大师给出了一种新的思路:
从大到小生成丑数,最小的丑数是1,而对于任意丑数x,其2*x,3*x,5*x也都是丑数。这样就可以用一个优先队列(自动排序)保存所有丑数。每次取最小丑数,生成3个新丑数。唯一需要注意的是,同一个丑数有多种生成方式,所以需要判断一个丑数是否已经生成过。
学习到刘大师的思路后自己写的代码:
<span style="font-size:14px;">#include <iostream>#include <vector>#include <queue>#include <set>#include <map>using namespace std;int main(){    typedef long long ll;    map<ll,bool> b;//用于记录数字是否出现过    const int pro[3]={2,3,5};//2.3.5的倍数(偷懒写法)    priority_queue<ll,vector<ll>,greater<ll> > a;//优先队列,之所以用它,是因为它的自动排序功能,具体如何规定它的排序方法等请见我的另一篇博文,网址在代码后    a.push(1);    b[1]=1;    int n=0;    while(++n){        ll x=a.top();        if(n==1500){            cout<<"The 1500'th ugly number is "<<x<<"."<<endl;            break;        }        a.pop();        for(int i=0;i<3;i++){            if(!b[x*pro[i]]){                a.push(x*pro[i]);                b[x*pro[i]]=1;            }        }    }}</span><span style="font-size:24px;"></span>
优先队列排序方法

 
0 0
原创粉丝点击