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. JUnit4:Test注解的两个属性:expected和timeout

    JUnit4:Test文档中的解释: The Test annotation supports two optional parameters. The first, expected, declar ...

  2. html 以及JavaScript 转义

    1.之前遇到一个问题,在用js给某个控件赋值时如果含有单引号'或者双引号"有可能导致赋值不成功,如document.getElementById("txtTestBox1" ...

  3. NSURLSession访问网络数据

    1.NSMutableURLRequest的设置 //创建NSMutableURLRequest对象 NSMutableURLRequest *request = [NSMutableURLReque ...

  4. ebs R12.2启动报错"failed to start a managed process after the maximum retry limit"

    启动日志: Error --> Process (index=1,uid=1739599208,pid=4479) failed to start a managed process after ...

  5. App.domain http->https

    App.domain = `${location.origin}/toa-mgw/rest/gateway`.replace(/http:\/\//, 'https://');

  6. 第十二章Fundamental Data Types 基本数据类型

    目录: 12.1 数值概论 12.2整数 12.3浮点数 12.4 字符和字符串 12.5布尔变量 12.6枚举类型 12.7具名常量 12.8数组 12.9创建你自己的类型 12.1   数值概论 ...

  7. python css基本操作

    1. 概述 css是英文Cascading Style Sheets的缩写,称为层叠样式表,用于对页面进行美化. 存在方式有三种:元素内联.页面嵌入和外部引入,比较三种方式的优缺点. 语法:style ...

  8. TortoiseGit上传项目到GitHub////////////////////////////z

    1.安装msysgit和TortoiseGit : 2.TortoiseGit 设置: (1).确保安装成功: (2).设置用户名和邮箱: 3.登陆github并进入设置页面: 4.添加 SSH Ke ...

  9. 彻底弄懂css中单位px和em,rem的区别

    国内的设计师大都喜欢用px,而国外的网站大都喜欢用em和rem,那么三者有什么区别,又各自有什么优劣呢? PX特点 -1. IE无法调整那些使用px作为单位的字体大小: -2. 国外的大部分网站能够调 ...

  10. Javascript-9-1-OOP-5-链式调用

    <html lang="en"> <head> </head> <body> <div class="" ...