hdoj-1058-Humble Numbers【DP&】

Humble Numbers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 20715    Accepted Submission(s): 9045

Problem 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

University of Ulm Local Contest 1996

Recommend

JGShining   |   We have carefully selected several similar problems for you:  1024 1025 1081 1160 2870

#include<stdio.h>
int dp[5900];
int min(int a,int b,int c,int d){
	int t1,t2;
	t1=a>b?b:a;
	t2=c>d?d:c;
	return t1>t2?t2:t1;
}
void f(){
	int f1,f2,f3,f4;
	f1=f2=f3=f4=1;
	dp[1]=1;
	for(int i=2;i<=5842;++i){
		dp[i]=min(dp[f1]*2,dp[f2]*3,dp[f3]*5,dp[f4]*7);
		if(dp[i]==dp[f1]*2) f1++;
		if(dp[i]==dp[f2]*3) f2++;
		if(dp[i]==dp[f3]*5) f3++;
		if(dp[i]==dp[f4]*7) f4++;
	}
}
int main(){
	f();
	int n;
	while(scanf("%d",&n),n){
		printf("The %d",n);
		if(n%10==1&&n%100!=11) printf("st");
		else if(n%10==2&&n%100!=12) printf("nd");
		else if(n%10==3&&n%100!=13) printf("rd");
		else printf("th");
		printf(" humble number is %d.\n",dp[n]);
	}
	return 0;
}

好题,刚开始没找到思路,看看别人的代码才明白,真是水啊

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-18 23:48:57

hdoj-1058-Humble Numbers【DP&】的相关文章

hdu 1058 Humble Numbers【dp】

题目链接:http://acm.acmcoder.com/showproblem.php?pid=1058 题意:数字只有2,3,5,7素因子的数叫做Humble Number,问你第 n 个Humble Number是什么? 解法:枚举. 代码: #include <stdio.h> #include <string.h> #include <vector> #include <string> #include <algorithm> #inc

hdoj 1492 The number of divisors(约数) about Humble Numbers 【数论】【质因子分解 求和】

定理:一个正整数 n 可以用素因子唯一表示为 p1^r1 * p2^r2 * ... pk^rk (其中 pi 为素数) , 那么这个数的因子的个数就是,(r1+1)*(r2+1)*...*(rk+1). 理解:为什么是加1之后再相乘,因为一个数的的因子数至少为1和他自身,但因为r1,r2..可以为0,所以因子的个数为(r1+1)... 拓展一下: 定理1: 一个正整数 n 可以用素因子唯一表示为 p1^r1 * p2^r2 * ... pk^rk (其中 pi 为素数) , 那么这个数的因子的

hdoj 2391 Filthy Rich 【DP】

题目大意:有个二维数组,你从(0,0)出发,最终到(n,m), 在这个二维数组中,每个位置dp[i][j]都有一定量的黄金,你可以拾取,问你最多能失去多少,并且,你的方向有下,右, 斜向下三个方向: 策略:就是每一个都加上它的上方向与左方向的最大值,这样到最后就是最大值.详情见代码 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2391 代码: #include<stdio.h> #include<string.h> int dp[1

HDU 1058 Humble Numbers(DP,数)

题意  所有只能被2,3,5,7这4个素数整除的数称为Humble Number  输入n  输出第n个Humble Number 1是第一个humble number  对于一个Humble Number  a  有2*a,3*a,5*a,7*a都是Humble Number  可以以1为基数  依次展开即可得到一定范围内的Humble Number 用i,j,k,l分别记录 2,3,5,7分别乘到了第几个Humble Number  当前在计算第cnt个Humble Number  那么有

HDU 1058.Humble Numbers【这个题怎么定位呢&#183;&#183;&#183;就【DP】吧】【8月28】

Humble Numbers Problem 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

HDU1058 Humble Numbers 【数论】

Humble Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 17407    Accepted Submission(s): 7565 Problem Description A number whose only prime factors are 2,3,5 or 7 is called a humble numb

HDU 1058 Humble Numbers(dp)

Problem 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 pri

hdoj 1421 搬寝室 【dp】

题意... 首先从小到大排个序,并且分析之后可得, 如果要去第i个的话,则第i-1个物品也要取(因为是排过序的与i相差最小的就是i-1或者是i+1, 但是i+1与i也可以看做i和i-1, 所以如果要去第i个的话,则第i-1个物品也要取). 分析:设dp[i][j]表示有i个物品,拿j对.则第i个物品对dp[i][j]有两种情况: 一:如果要不取第i个物品, 则此时的dp[i][j] = dp[i-1][j](仔细考虑一下) 二:如果要取第i个物品, 则此时有dp[i][j] = dp[i-2][

hdoj 2046 骨牌铺方格 【DP】+【斐波那契】

dp果然不是好学的... 第n个,即2*n时,可由第n-1个的竖直排列再加一个,和第n-2个中横着排两个 所以f(n) = 1×f(n-1) + 1×f(n-2): 骨牌铺方格 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 28412    Accepted Submission(s): 13771 Problem Descripti