DP 水题


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

1

2

3

4

11

12

13

21

22

23

100

1000

5842

0

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

Ulm Local 1996


题目大意

一个数,如果它的质因子只有2,3,5,7,则它是一个:Humble Numbers。输入一个n,输出第n个Humble Numbers。

题解

对于一个数,因为质因子只有2,3,5,7,所以它肯定是由前面,某个数乘2,3,5,7得来的。

所以定义dp[i] 表示第i个数,转移方程为:

Dp[i] = min{dp[f2] * 2, dp[f3] * 3, dp[f5] * 5, dp[f7] * 7};

F2,f3,f5,f7是dp[]中的下标。dp[1] = 1;

代码

#include <iostream>
#include <cstdio>
using namespace std; const int maxn = 5842;
int dp[maxn + 1]; int main() {
int f2 = 1, f3 = 1, f5 = 1, f7 = 1;
dp[1] = 1;
int i = 1;
while(i++ < maxn) {
dp[i] = min(min(dp[f2]*2, dp[f3]*3), min(dp[f5]*5,dp[f7]*7));
if(dp[i] == dp[f2] * 2)f2++;
if(dp[i] == dp[f3] * 3)f3++;
if(dp[i] == dp[f5] * 5)f5++;
if(dp[i] == dp[f7] * 7)f7++;
}
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]);
} }

[poj2247] Humble Numbers (DP水题)的更多相关文章

  1. HDOJ(HDU).1058 Humble Numbers (DP)

    HDOJ(HDU).1058 Humble Numbers (DP) 点我挑战题目 题意分析 水 代码总览 /* Title:HDOJ.1058 Author:pengwill Date:2017-2 ...

  2. ACM :漫漫上学路 -DP -水题

    CSU 1772 漫漫上学路 Time Limit: 1000MS   Memory Limit: 131072KB   64bit IO Format: %lld & %llu Submit ...

  3. Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题

    除非特别忙,我接下来会尽可能翻译我做的每道CF题的题面! Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题 题面 胡小兔和司公子都认为对方是垃圾. 为了决出谁才是垃 ...

  4. DP 60题 -3 HDU1058 Humble Numbers DP求状态数的老祖宗题目

    Humble Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  5. HDU 1058 Humble Numbers (DP)

    Humble Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  6. 13年山东省赛 The number of steps(概率dp水题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud The number of steps Time Limit: 1 Sec  Me ...

  7. PAT甲题题解-1120. Friend Numbers (20)-水题

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789775.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  8. dp水题 序列问题 (9道)

    9道题.A了8道,A题看题解也没弄懂怎么维护m段子序列的,过一段时间再回来看看     dp试水 47:56:23 125:00:00   Overview Problem Status Rank ( ...

  9. 【BZOJ】1270: [BeijingWc2008]雷涛的小猫(DP+水题)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1270 这完全是一眼题啊,但是n^2的时间挺感人.(n^2一下的级别请大神们赐教,我还没学多少dp优化 ...

随机推荐

  1. Apache 配置多端口

    Apache 配置多端口,主要是以下步骤 1. 如果电脑是64位的,官网上下载WampServe,装的过程中如果出现msvcp110.dll丢失的话,解决办法如下: 1.1 首先是打开浏览器,在浏览器 ...

  2. ubuntu 更改时区

    更改/etc/timezone Asia/Chongqing sudo dpkg-reconfigure tzdata

  3. iOS:崩溃统计工具Crashlytics的使用

    一.介绍 随着苹果在世界火热起来,移动端app的开发如火如荼,同时催生出了一批对app进行统计的开发工具,诸如:国内的友盟统计.国外的Flurry移动端统计.国外的Crashlytics统计等,Cra ...

  4. Docker version 1.12.5建立registry私库

    sudo docker run -d -p 5000:5000 -v /opt/data/registry:/var/lib/registry registry :前面的是宿主机的地址(/opt/da ...

  5. js实现下滑加载更多的效果

    var stop=true;$(window).scroll(function(){ totalheight = parseFloat($(window).height()) + parseFloat ...

  6. python中常用的模块的总结

    1. 模块和包 a.定义: 模块用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件.(例如:文件名:test.py,对应的模块名:test) ...

  7. word嵌入图片部分被段落遮挡

    这是因为图片所在段落的行距被设置为固定行距造成的 解决办法: 把图片所在段落的行距改为单倍行距

  8. c语言1

    1.c语言的执行过程: 源代码:source code,使用某种计算机语言书写的代码. 编译:compile 对源代码进行翻译的过程. 编译器:compiler 负责编译工具. gcc:一款通用的编译 ...

  9. mysql 几个命令

    show databases; 显示所有数据库 create database 数据库名; 创建数据库 drop database 数据库名; 删除数据库

  10. hduoj 1285 确定比赛名次

    http://acm.hdu.edu.cn/showproblem.php?pid=1285 确定比赛名次 Time Limit: 2000/1000 MS (Java/Others) Memory ...