[poj2247] Humble Numbers (DP水题)
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水题)的更多相关文章
- HDOJ(HDU).1058 Humble Numbers (DP)
HDOJ(HDU).1058 Humble Numbers (DP) 点我挑战题目 题意分析 水 代码总览 /* Title:HDOJ.1058 Author:pengwill Date:2017-2 ...
- ACM :漫漫上学路 -DP -水题
CSU 1772 漫漫上学路 Time Limit: 1000MS Memory Limit: 131072KB 64bit IO Format: %lld & %llu Submit ...
- Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题
除非特别忙,我接下来会尽可能翻译我做的每道CF题的题面! Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题 题面 胡小兔和司公子都认为对方是垃圾. 为了决出谁才是垃 ...
- DP 60题 -3 HDU1058 Humble Numbers DP求状态数的老祖宗题目
Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- HDU 1058 Humble Numbers (DP)
Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- 13年山东省赛 The number of steps(概率dp水题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud The number of steps Time Limit: 1 Sec Me ...
- PAT甲题题解-1120. Friend Numbers (20)-水题
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789775.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- dp水题 序列问题 (9道)
9道题.A了8道,A题看题解也没弄懂怎么维护m段子序列的,过一段时间再回来看看 dp试水 47:56:23 125:00:00 Overview Problem Status Rank ( ...
- 【BZOJ】1270: [BeijingWc2008]雷涛的小猫(DP+水题)
http://www.lydsy.com/JudgeOnline/problem.php?id=1270 这完全是一眼题啊,但是n^2的时间挺感人.(n^2一下的级别请大神们赐教,我还没学多少dp优化 ...
随机推荐
- 获取ip ,百度地图坐标点 和 在 后台调用 url()
protected void getip() { string ips = HttpContext.Current.Request.UserHostA ...
- 【原创】Android selector选择器无效或无法正常显示的一点研究
想将LinearLayout作为一个按钮,加上一个动态背景,按下的时候,背景变色,这个理所当然应该使用selector背景选择器来做: <LinearLayout android:id=&quo ...
- svn 架设
1.yum install subversion openssl-devel -y 2. cd /data/svn 3. svnadmin create remote 4. 编辑conf 下 aut ...
- (转载)solr时区问题解决方案
solr默认的使用的是utc格林尼治时间,与我们的GMT+8相差8个小时,网上好多解决办法是在自己应用中的时间上加8个小时和减8个小时做变换:或者不用date类型,改为long. 个人感觉这两个办法都 ...
- AngularJS 2.0
https://angular.io/docs/ts/latest/guide/learning-angular.html QuickStart: git clone https://github.c ...
- Mac iTerm2命令行快捷操作
control + R 搜索之前输入过的命令 control + U 删除整行命令 control + W 删除光标前面的命令 control + K 删除光标后面的命令
- R12.2 URL Validation failed. The error could have been caused through the use of the browser's navigation buttons
EBS升级到R12.2.4后,进入系统操作老是报以下错误: 通过谷歌发现有人遇到相同的问题,并提供了解决方案. 原文地址:http://onlineappsdbaoracle.blogspot.com ...
- NPOI 自定义单元格背景颜色-Excel
NPOI针对office2003使用HSSFWorkbook,对于offce2007及以上使用XSSFWorkbook:今天我以HSSFWorkbook自定义颜色为例说明,Office2007的未研究 ...
- MWeb 2.0.7 版发布!
更新前针对 MAS 上的评论重点说一下:MWeb 是支持直接对本地文件夹操作的,不用导入到文档库!请使用外部模式!请使用外部模式!!请使用外部模式!!! 重要的话讲三次!使用方法是 CMD + E 打 ...
- Java 高精度数字
BigInteger // 高精度整数 BigDecimal //高精度小数 小数位数不受限制