HDOJ 1058 Humble Numbers(基础DP方程)

来源:互联网 发布:java app接口怎么写 编辑:程序博客网 时间:2024/05/19 23:29

Humble Numbers

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



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.题意:定义一种数humble number,这种数只有2,3,5,7,四种因子,将将自然数中所有的这种数升序排序,现在给你一个数 n ,问所有的humble number中的第 n 个是多少,n满足1<=n<=5842,注意规定第一个humble number是 1。注意答案的输出格式。题解:简单的DP,明显由样例可以看出5842个humble number是2000000000,要是从一开始枚举加判断的话必然会TLE,所以这时能发现个数只有5842,所以可以用DP直接找出所有的humble number,因为题中已经规定第一个humble number是1,设为dp[1]=1这时 dp[2]=min{dp[1]*2,dp[1]*3,dp[1]*5,dp[1]*7},这时显然dp[2]=dp[1]*2,这样dp[3]=min{dp[2]*2,dp[1]*3,dp[1]*5,dp[1]*7},一次类推,即上一次选的谁下一次就把它的dp[i]中的 i 加 1 ,这样就能找出所有的5842个humble number,这里还要注意一点就是最后的输出格式,下面来看一下详细的代码。
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1058
代码:
#include<iostream>#include<cstdio>#include<cmath>#include<cstring>using namespace std;const int maxn=6000;int dp[maxn];int main(){    dp[1]=1;//初始化dp[1]=1;    int p2=dp[1]*2;int p3=dp[1]*3;//初始动态转移中的四个数    int p5=dp[1]*5;int p7=dp[1]*7;    int t2=1,t3=1,t5=1,t7=1;//初始动态转移中的四个数的dp[i]中的i    for(int i=2;i<=5842;){//找出2-5842个humble number        int p=p2,pm=t2;//p为四个数中的最小的数,pm为记录的dp[i]中的i        int flg=2;//标记那个数是最小的,标记下来        //找出p,pm,flg        if(p3<p) flg=3,p=p3;        if(p5<p) flg=5,p=p5;        if(p7<p) flg=7,p=p7;        if(p>dp[i-1]) dp[i++]=p;        //更新p2,p3,p5,p7,dp[i],        if(flg==2) t2++,p2=p2/dp[t2-1]*dp[t2];        else if(flg==3) t3++,p3=p3/dp[t3-1]*dp[t3];        else if(flg==5) t5++,p5=p5/dp[t5-1]*dp[t5];        else if(flg==7) t7++,p7=p7/dp[t7-1]*dp[t7];    }    int n;    while(scanf("%d",&n),n){        //注意输出格式        if(n%10==1&&n%100!=11) printf("The %dst humble number is %d.\n",n,dp[n]);        else if(n%10==2&&n%100!=12) printf("The %dnd humble number is %d.\n",n,dp[n]);        else if(n%10==3&&n%100!=13) printf("The %drd humble number is %d.\n",n,dp[n]);        else printf("The %dth humble number is %d.\n",n,dp[n]);    }    return 0;}


0 0
原创粉丝点击