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. visual studio2015从git上clone(克隆)项目

    本文介绍Visual Studio2015从git上clone项目代码的步骤. 1.打开VS2015,进到起始页,打开"团队资源管理器",如下图: 2.点击"克隆&quo ...

  2. jqurey datatable tableTools 自定义button元素 以及按钮自事件

    版本 1.10.4 "dom": 'T<"clear">lfrtip', "tableTools": { //"sSw ...

  3. laravel5.1学习(2)-- artisan tinker命令

    例如:为users表创建20条测试输入 G:\wamp\www\hcmf>php artisan tinker >>> namespace App; => null &g ...

  4. Java网络编程之流——流、过滤器、阅读器和书写器

    Java的I/O建立于流(Stream)之上.输入流读取数据:输出流写入数据.所有的输出流都有相同的基本方法来写入数据,所有输入流也使用相同的基本方法来读取数据.在创建流之后,你通常可以忽略在读写时的 ...

  5. C语言第五次作业

    #include<stdio.h> int main() { int a,b,c; printf("请输入3个整数:"); scanf("%d %d %d&q ...

  6. php7.0.12 laravel 链接sqlserver数据库

    https://www.microsoft.com/en-us/download/details.aspx?id=20098 下载最后一个,然后这个工具可以将dll扩展下载下来,选择一个空白的文件夹就 ...

  7. z-index

    1.定位对象能够遮盖非定位对象,除非定位对象的z-index值为负数. 2.同层级定位对象z-index大的能够覆盖z-index值小的. 2.1  同层级的子集相互比较要先看父集(即同层级)的大小; ...

  8. 存储过程中的output跟return区别及实例说明

    存储过程return,表示该存储过程执行到当当前return位置,不再向下执行: 存储过程写法:set ANSI_NULLS ON set QUOTED_IDENTIFIER ON GO ALTER ...

  9. paper 120:计算距离矩阵的函数的pdist和pdist2函数

    matlab中自带的计算距离矩阵的函数有两个pdist和pdist2.前者计算一个向量自身的距离矩阵,后者计算两个向量之间的距离矩阵.基本调用形式如下: D = pdist(X) D = pdist2 ...

  10. crontab每秒执行URL接口

    首先crontab -e打开进行编辑 添加以下代码(默认为每秒执行一次脚本crontab.sh): * * * * * /bin/sh /var/www/aa/crontab.sh 下面是/var/w ...