HDU 1058 Humble Number
来源:互联网 发布:mac下安装linux虚拟机 编辑:程序博客网 时间:2024/05/16 14:40
题意大概是,2,3,5,7的和他们的整数倍数的数都称为Humble Number,将所有的Humble Number从小到大排列,现在输入N(N <= 5842)输出第N个Humble Number
考虑到一个一个数是Humble Number,那么他的2,3,5,7倍一定也是HUmble Number,因此只要想办法处理怎么样让其从小到大一个一个输出就好。
网上看到一个比较巧妙的办法,在这里记录下来
设f(n)为第n个Humble Number
那么有f(n) = min(f(l1)*2,f(l2)*3,f(l3)*5,f(l4)*7),初始l1=l2=l3=l4=1
处理完一个数之后相应的值往后挪一个
#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>using namespace std;typedef long long LL;#define min_t(a,b) (((a)<(b))?(a):(b))LL min(LL a,LL b,LL c,LL d) { return min_t(min_t(a,b),min_t(c,d));} LL dp[6000];void init() { int l1 = 1,l2 = 1,l3 = 1,l4 = 1; dp[1] = 1; for(int i = 2;i <= 5842;i++) { dp[i] = min(dp[l1] * 2,dp[l2] * 3,dp[l3] * 5,dp[l4] * 7); if(dp[i] == dp[l1] * 2) l1++; if(dp[i] == dp[l2] * 3) l2++; if(dp[i] == dp[l3] * 5) l3++; if(dp[i] == dp[l4] * 7) l4++; }}int main() { int n; init(); while(scanf("%d",&n),n) { cout << "The " << n; if(n % 100 / 10 == 1) cout << "th"; else if(n % 10 == 1) cout << "st"; else if(n % 10 == 2) cout << "nd"; else if(n % 10 == 3) cout << "rd"; else cout << "th"; cout << " humble number is " << dp[n] << "." << endl; } return 0;}
0 0
- humble number hdu 1058
- hdu 1058 humble number
- HDU 1058 Humble Number
- hdu 1058 Humble Number
- hdu 1058 humble number
- hdu 1058 humble number
- HDU 1058 Humble Number DP
- HDU 1058 & POJ 2247 Humble Number
- hdoj 1058 Humble Number
- 丑数(humble number) hdu 1058 DP
- HDU 1058 Humble Number(打表dp)
- HDU 1058 Humble Numbers
- hdu 1058 Humble Numbers
- hdu 1058 Humble Numbers
- HDU 1058 Humble Numbers
- hdu 1058 Humble Numbers
- HDU 1058 Humble Numbers
- hdu 1058 humble numbers
- UvaLive 2965 中途相遇法
- Poj 1082 食物链 带权值的并查集
- 面试-数据库锁
- hdu 1040
- HDU 1271 整数对
- HDU 1058 Humble Number
- HDU 1081 To The Max
- HDU 1506 Largest Rectangle in a Histogram
- HDU 1421 搬寝室
- 大家好、新人一枚 报道 报道
- AOJ 47 字符串匹配
- LA3026 Period
- HDU 1078 FatMouse and Cheese
- CentOS安装qt4.8