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. ubuntu下的时间设定(硬件时间,系统时间,本地时间)

    问题的来由是在这里: 在cron里设定任务是在凌晨6点执行,检查日志时发现时间总是不对,是在22点左右的时间执行的.研究发现,任务是在本地时间的6点执行了,但不知为什么syslog中的时间都是为utc ...

  2. springmvc 动态代理 JDK实现与模拟JDK纯手写实现。

    首先明白 动态代理和静态代理的区别: 静态代理:①持有被代理类的引用  ② 代理类一开始就被加载到内存中了(非常重要) 动态代理:JDK中的动态代理中的代理类是动态生成的.并且生成的动态代理类为$Pr ...

  3. spring使用cache

    考虑两方面: i) 声明某些方法使用缓存(注解方式) ii) 配置Spring对Cache的支持. 一.基于注解的支持 一般我们常用的注解:@Cacheable和@CacheEvict. 1.1.@C ...

  4. NuGet控制台有几个常用命令

    NuGet控制台有几个常用命令 Get-Package 获取当前项目已经安装的类库 Install-Package 安装指定类库,命令格式如下:Install-Package 类库ID,如Instal ...

  5. Trace-语句启动Profiler中暂停的跟踪会出现什么状况

    2016-09-08 22:09 整理,未发布Profiler创建客户端跟踪.常规页不保存文件.不勾选服务器处理跟踪数据:事件选择RPC:Completed和SQL:BatchCompleted,列筛 ...

  6. 使用linq的好处

    1.linq非常方便,把复杂的业务逻辑从数据库分离,起到了很好的优化作用 2.linq非常灵活,可以用基本统一的访问方式,访问各种数据源,对项目的管理和维护,起到了十分便捷的作用 3.用linq可以不 ...

  7. Oracle中较长number型数值的科学计数显示问题

    表中有id列,类型为number(38).在sqlplus中查询的时候,查询结果的显示方式为科学计数法: ID ---------- 4.5572E+18 4.5574E+18 4.5585E+18 ...

  8. windows下搭建nginx+php+mysql环境

    一.下载需要的东西 1.nginx:http://nginx.org/en/download.html 2.php:http://php.net/downloads.php 3.mysql:(暂时先不 ...

  9. 用Netty开发中间件:高并发性能优化

    用Netty开发中间件:高并发性能优化 最近在写一个后台中间件的原型,主要是做消息的分发和透传.因为要用Java实现,所以网络通信框架的第一选择当然就是Netty了,使用的是Netty 4版本.Net ...

  10. poj 2376 Cleaning Shifts

    http://poj.org/problem?id=2376 Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...