HDU Problem - 1058 Humble Numbers 【dp】

 

Humble Numbers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24415    Accepted Submission(s): 10702

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
 

 

Recommend
JGShining   |   We have carefully selected several similar problems for you:  1069 1421 1024 1081 1505 

 

 

这道题主要是前面的打表部分,一开始想用一个4层循环解决,但是乘积会爆掉long long,然后百度知道用dp做。

#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
#include <stack>
#include <cmath>
#include <string>
#include <vector>
#include <cstdlib>
//#include <bits/stdc++.h>
//#define LOACL
#define space " "
using namespace std;
typedef long long LL;
//typedef __int64 Int;
typedef pair<int, int> paii;
const int INF = 0x3f3f3f3f;
const double ESP = 1e-5;
const double PI = acos(-1.0);
const int MOD = 2000000000 + 7;
const int MAXN = 10000 + 10;
LL ans[MAXN];
int p2 = 1, p3 = 1, p5 = 1, p7 = 1;
LL min_f(LL x1, LL x2, LL x3, LL x4) {
    LL minn = min(min(x2, x1), min(x3, x4));
    //下面的式子不可以是if else 结构
    if (minn == x1) p2++;
    if (minn == x2) p3++;
    if (minn == x3) p5++;
    if (minn == x4) p7++;
    return minn;
}
void init() {
    ans[1] = 1LL;
    for (int i = 2; i <= 5842; i++) {
        ans[i] = min_f(ans[p2]*2LL, ans[p3]*3LL, ans[p5]*5LL, ans[p7]*7LL);
    }
}
int main() {
    init(); int n;
    while (scanf("%d", &n), n) {
        if (n%100 != 11 && n%10 == 1) printf("The %dst", n);
        else if (n % 100 != 12 && n % 10 == 2) printf("The %dnd", n);
        else if (n % 100 != 13 && n % 10 == 3)  printf("The %drd", n);
        else  printf("The %dth", n);
        printf(" humble number is %lld.\n", ans[n]);
    }
    return 0;
}

 


 

posted @ 2016-11-12 15:15  zprhhs  阅读(137)  评论(0编辑  收藏  举报
Power by awescnb