http://poj.org/problem?id=1338

第一反应就是DP,DP[i] = min{2*DP[j], 3*DP[k], 5*DP[p] j,k,p<i};于是枚举一下0~i-1即可

后来听到室友说,可以通过上一个×2、×3、×5得到。于是搞了个优先队列预处理。

后来看了一下以前A的代码。O(n)的。。。(虽然不是我自己做出的= =)

时间都是0Ms

O(n^2)和O(nlogn)的:

 #include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define MAX(a,b) (a > b ? a : b)
#define MIN(a,b) (a < b ? a : b)
#define mem0(a) memset(a,0,sizeof(a)) typedef long long LL;
const double eps = 1e-;
const int MAXN = ;
const int MAXM = ; struct NODE
{
int num;
int flag;
NODE(){}
NODE(int _num, int _flag){num=_num;flag=_flag;}
bool operator < (NODE B)const
{
return num > B.num;
}
};
int DP[], N; //void init()
//{
// int time = 0;
// for(int i=1;i<=1500;i++) DP[i] = INF;
// DP[1] = 1; int now = 1;
// int two=1, three=1, five=1;
// for(int i=2;i<=1500;i++)
// {
// for(int j=min(two,min(three,five));j<i;j++)
// {
// time ++;
// if(DP[j]*2 > now && DP[i]>DP[j]*2){ DP[i] = min(DP[i], DP[j]*2); two = j;break;}
// if(DP[j]*3 > now && DP[i]>DP[j]*3){ DP[i] = min(DP[i], DP[j]*3); three = j;}
// if(DP[j]*5 > now && DP[i]>DP[j]*5){ DP[i] = min(DP[i], DP[j]*5); five = j;}
// }
// now = DP[i];
// }
// //printf("Time=%d\n", time);
//} void init()
{
priority_queue<NODE>q;
NODE U; U.num=; U.flag=-;
q.push(U);
int num = , tot = ;
while()
{
U = q.top(); q.pop();
DP[num++] = U.num;
if(num>) return ;
if(tot > ) continue;
q.push(NODE(U.num*, )); tot++;
if(U.flag<=) {q.push(NODE(U.num*, )); tot++; }
if(U.flag<=-){q.push(NODE(U.num*, -)); tot++; }
}
} int main()
{
// printf("%d\n", (int)(1600 * (log(1600.0)/log(2.0))));
// freopen("test.in", "r", stdin);
init();
while(~scanf("%d", &N) &&N)
{
printf("%d\n", DP[N]);
}
return ;
}

O(n)的:不是我写的= =

 #include <stdio.h>
int min(int a,int b,int c)
{
if(b<a)
a=b;
if(c<a)
a=c;
return a;
}
int main()
{
int n;
int i2_mul;
int i3_mul;
int i5_mul;
unsigned long ugly[]; i2_mul = ;
i3_mul = ;
i5_mul = ;
ugly[]=; for( int i = ; i <= ; i++ )
{
ugly[i] = min(ugly[i2_mul]*,ugly[i3_mul]*,ugly[i5_mul]*);
if(ugly[i] == ugly[i2_mul]* )
i2_mul++;
if(ugly[i] == ugly[i3_mul]* )
i3_mul++;
if(ugly[i] == ugly[i5_mul]*)
i5_mul++; } while(true)
{
scanf("%d",&n); if( n == )
break; printf("%d\n",ugly[n]); } return ;
}

POJ1338Ugly Numbers(DP)的更多相关文章

  1. Gym 100703G---Game of numbers(DP)

    题目链接 http://vjudge.net/contest/132391#problem/G Description standard input/outputStatements — It' s ...

  2. URAL 1586 Threeprime Numbers(DP)

    题目链接 题意 : 定义Threeprime为它的任意连续3位上的数字,都构成一个3位的质数. 求对于一个n位数,存在多少个Threeprime数. 思路 : 记录[100, 999]范围内所有素数( ...

  3. Codeforces 403D: Beautiful Pairs of Numbers(DP)

    题意:转换模型之后,就是1~n个数中选k个,放到一个容量为n的背包中,这个背包还特别神奇,相同的物品摆放的位置不同时,算不同的放法(想象背包空间就是一个长度为n的数组,然后容量为1的物体放一个格子,容 ...

  4. 【gym102394B】Binary Numbers(DP)

    题意:From https://blog.csdn.net/m0_37809890/article/details/102886956 思路: 可以发现转移就是右上角的一个区间前缀和 std只要开1倍 ...

  5. 【CF55D】Beautiful numbers(动态规划)

    [CF55D]Beautiful numbers(动态规划) 题面 洛谷 CF 题解 数位\(dp\) 如果当前数能够被它所有数位整除,意味着它能够被所有数位的\(lcm\)整除. 所以\(dp\)的 ...

  6. LightOJ 1033 Generating Palindromes(dp)

    LightOJ 1033  Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  7. lightOJ 1047 Neighbor House (DP)

    lightOJ 1047   Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...

  8. UVA11125 - Arrange Some Marbles(dp)

    UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...

  9. Humble Numbers(hdu1058)

    Humble Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

随机推荐

  1. bzoj2982: combination

    借(cao)鉴(xi)自popoqqq大爷的lucas定理的写法 #include<cstdio> #include<cstring> #include<cctype&g ...

  2. 搜索浅谈(Elasticsearch和Lucene4分享)

    刚刚过去的双11,真是给线下运营商好好上了一课.当今的互联网真是炙手可热,大家对互联网的热情是如此之高.相信电商之间的竞争将更加的激烈和残酷,不过,搜索,作为用户体验很重要的一点,各大电商也做的越来越 ...

  3. [转] POJ字符串分类

    POJ 1002 - 487-3279(基础)http://acm.pku.edu.cn/JudgeOnline/problem?id=1002题意:略解法:二叉查找数,map,快排... POJ 1 ...

  4. 创建一个进程并调用(.net)

    最近有一个项目需求,需要调用一个exe,就上网查询了一下,顺利的完成了工作,感觉虽然简单,但挺有意思,就记录一下. 一,创建一个进程 1,代码视图(控制台程序) 2,代码 using System; ...

  5. UVA 489 Hangman Judge (字符匹配)

    题意:给一个字符串A,只含小写字符数个.再给一个字符串B,含小写字符数个.规则如下: 1.字符串B从左至右逐个字符遍历,对于每个字符,如果该字符在A中存在,将A中所有该字符删掉,若不存在,则错误次数+ ...

  6. strust2 配置chainAction结果类型的配置

    <result name="chainAction" type="chain"> <param name="actionName&q ...

  7. spring整合各大ORM框架的原理图

  8. Android Studio Check for Update

    Android Studio 当前版本1.0.1, 官网新版本1.1.0, 通过 Check for Update...升级, 提示 Connection failed. Please check y ...

  9. centos5.4_x64 red5 1.0安装部署

    一.centos5.4_x64 默认安装后会集成jdk1.6 java -version 如果显示当前安装的版本号为1.6,则不需要再安装. 二.安装Red5 1.创建临时目录: cd /usr/lo ...

  10. ACE的 日志

    http://wenku.baidu.com/link?url=dK6j9_0pICRjxWW7usBlkCxPTa8zFSPyUe_uWAkwMPFDU4ip_tEfxpOitxjkl3RuPy3D ...