hdu-acm stepsHumble Numbers

这是我做的第六道动态规划水题,对动态规划差不多有了一个大致的概念。动态规划有几个关键因素,第一是最优子结构,第二是状态和状态转移方程。整个过程都是以  最优  为中心的。因此在状态转移方程中常涉及到几个子状态的最优化的判断。这道题既采用了递堆的思想,又采用了一点动态规划的思想。状态转移方程为:f[i]=min{2*f[p],3*f[q],5*f[r],7*f[s]};

 1 #include"iostream"
 2 #include"stdio.h"
 3 #include"algorithm"
 4 #include"string.h"
 5 #include"cmath"
 6 #include"ctype.h"
 7 #define mx 10005
 8 using namespace std;
 9 long long dp[mx];
10 int p,q,r,s;
11 int min(int a,int b,int c,int d)
12 {
13     int mi=a;
14     if(b<mi) mi=b;
15     if(c<mi) mi=c;
16     if(d<mi) mi=d;
17
18     if(a==mi) p++;
19     if(b==mi) q++;
20     if(c==mi) r++;
21     if(d==mi) s++;
22
23     return mi;
24 }
25 int main()
26 {
27     int i,n;
28     dp[1]=1;
29     p=q=r=s=1;
30     for(i=2;i<=5842;i++)
31     {
32         dp[i]=min(2*dp[p],3*dp[q],5*dp[r],7*dp[s]);
33     }
34     while(cin>>n,n)
35     {
36         if(n%10==1&&n%100!=11) cout<<"The "<<n<<"st humble number is "<<dp[n]<<"."<<endl;
37         else if(n%10==2&&n%100!=12) cout<<"The "<<n<<"nd humble number is "<<dp[n]<<"."<<endl;
38         else if(n%10==3&&n%100!=13) cout<<"The "<<n<<"rd humble number is "<<dp[n]<<"."<<endl;
39         else cout<<"The "<<n<<"th humble number is "<<dp[n]<<"."<<endl;
40     }
41     return 0;
42 }

时间: 2024-08-25 05:57:25

hdu-acm stepsHumble Numbers的相关文章

HDU 3117 Fibonacci Numbers(斐波那契前后四位,打表+取对+矩阵快速幂)

HDU 3117 Fibonacci Numbers(斐波那契前后四位,打表+取对+矩阵快速幂) ACM 题目地址:HDU 3117 Fibonacci Numbers 题意: 求第n个斐波那契数的前四位和后四位. 不足8位直接输出. 分析: 前四位有另外一题HDU 1568,用取对的方法来做的. 后四位可以用矩阵快速幂,MOD设成10000就行了. 代码: /* * Author: illuz <iilluzen[at]gmail.com> * Blog: http://blog.csdn.

杭电 HDU ACM 1397 Goldbach&#39;s Conjecture

Goldbach's Conjecture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4976    Accepted Submission(s): 1901 Problem Description Goldbach's Conjecture: For any even number n greater than or equal

杭电 HDU ACM 5186 zhx&#39;s submissions

zhx's submissions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1892    Accepted Submission(s): 507 Problem Description As one of the most powerful brushes, zhx submits a lot of code on many

hdu 4722 Good Numbers(初涉数位dp)

http://acm.hdu.edu.cn/showproblem.php?pid=4722 大致题意:若一个整数的各位数字之和是10的倍数,称这个数为"good number".给出区间[A,B],求出该区间内"good number"的数的个数. 第一道数位dp,折腾了半天才明白怎么回事. 设dp[site][mod]表示到第site位(由高位向低位)前面各位数字之和对10取余为mod的数的个数,进行记忆化搜索.有两个很重要的点,首先是变量up,表示是否到达边界

HDU 4722 Good Numbers (数位dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4722 思路:数位dp,dp[i][j]表示到第i位,数字和%10为j,然后进行dp,注意完全匹配的情况是要+1,而其他情况是从0 到 9 都要考虑 代码: #include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> using namespace std; int

hdu 3117 Fibonacci Numbers

点击此处即可传送到hdu 3117 **Fibonacci Numbers** Problem Description The Fibonacci sequence is the sequence of numbers such that every element is equal to the sum of the two previous elements, except for the first two elements f0 and f1 which are respectively

HDU ACM 1103 Flo&#39;s Restaurant

分析:借助STL的min_element实现.每次更新最先被占用的桌子,具体见注释. #include<iostream> #include<algorithm> using namespace std; int main() { int A,B,C; char s[10]; int a[102],b[102],c[102]; int curtime,count,ans; int *p; //桌子最先空闲时间 while(cin>>A>>B>>C

hdu acm 1425 sort(哈希表思想)

sort Time Limit: 6000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25803    Accepted Submission(s): 7764 Problem Description 给你n个整数,请按从大到小的顺序输出其中前m大的数. Input 每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个各不相同,且

HDU ACM 1005 Number Sequence

Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 119732    Accepted Submission(s): 29072 Problem Description A number sequence is defined as follows:f(1) = 1, f(2) = 1, f(n) = (A

hdu acm 1166 敌兵布阵 (线段树)

敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 37903    Accepted Submission(s): 15985 Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务