HDU_1058 && HDU_3199(指针 + dp)

来源:互联网 发布:于正 知乎 编辑:程序博客网 时间:2024/06/06 10:58

Humble Numbers

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


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
1234111213212223100100058420
 

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.
 

题意:素因子只有2、 3、 5或7的数被称为humble数。现在要得到第n个humble数是多少。

分析:一个新的数产生,肯定是min(x * 2, y * 3, z * 5, w * 7),现在是x, y, z, w如何确定。我们可以初始化dp[1] = 1,然后设置4个指针p1 = p2 = p3 = p4 = 1,然后呢,刚刚提到的x, y, z, w就分别对应dp[p1], dp[p2], dp[p3], dp[p4],也就是说dp[next] = min(dp[p1] * 2, dp[p2] * 3, dp[p3] * 5, dp[p4] * 7),最小的指针对应+1,直到生成所需的数。

值得一提的是,st, nd, rd, th这几个我一开始没分清……

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1058

代码清单:

#include <queue>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn = 5842 + 5;int n;int dp[maxn];void initTable() {dp[1] = 1;int p1 = 1, p2 = 1, p3 = 1, p4 =1;for(int i = 2; i <= 5842; ++i) {dp[i] = min(min(dp[p1] * 2, dp[p2] * 3), min(dp[p3] * 5, dp[p4] * 7));if(dp[i] == dp[p1] * 2) p1 += 1;if(dp[i] == dp[p2] * 3) p2 += 1;if(dp[i] == dp[p3] * 5) p3 += 1;if(dp[i] == dp[p4] * 7) p4 += 1;}}void solve() {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]);}int main() {initTable();while(scanf("%d", &n) != EOF && n) {solve();}return 0;}

Hamming Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1246    Accepted Submission(s): 545


Problem Description
For each three prime numbers p1, p2 and p3, let's define Hamming sequence Hi(p1, p2, p3), i=1, ... as containing in increasing order all the natural numbers whose only prime divisors are p1, p2 or p3. 

For example, H(2, 3, 5) = 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 25, 27, ... 

So H5(2, 3, 5)=6.
 

Input
In the single line of input file there are space-separated integers p1 p2 p3 i.
 

Output
The output file must contain the single integer - Hi(p1, p2, p3). All numbers in input and output are less than 10^18.
 

Sample Input
7 13 19 100
 

Sample Output
26590291
 
分析:和上面那题差不多,就是数据范围给得吓人,其实并没有那么大。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3199

代码清单:

#include <queue>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn = 1e6 + 5;long long dp[maxn];long long p1, p2, p3;int id;void solve() {dp[0] = 1;int i1 = 0, i2 = 0, i3 = 0;for(int i = 1; i <= id; ++i) {dp[i] = min(dp[i1] * p1, min(dp[i2] * p2, dp[i3] * p3));if(dp[i] == dp[i1] * p1) i1 += 1;if(dp[i] == dp[i2] * p2) i2 += 1;if(dp[i] == dp[i3] * p3) i3 += 1;}printf("%lld\n", dp[id]);}int main() {while(scanf("%lld%lld%lld%d", &p1, &p2, &p3, &id) != EOF) {solve();}}



Source
0 0
原创粉丝点击