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. linux shared lib 使用与编译

    一.              动态链接库的原理及使用 Linux提供4个库函数.一个头文件dlfcn.h以及两个共享库(静态库libdl.a和动态库libdl.so)支持动态链接. Ø        ...

  2. 代码记录:使用Aforge.net让视频图像反转180度

    private void CameraConn() { videoSource = new VideoCaptureDevice(videoDevices[tscbxCameras.SelectedI ...

  3. 20160205.CCPP体系详解(0015天)

    程序片段(01):01.杨辉三角.c 内容概要:杨辉三角 #include <stdio.h> #include <stdlib.h> #define N 10 //01.杨辉 ...

  4. 使用jQuery Mobile实现新闻浏览器(3)

    在本教程的前两篇文章中,笔者分别向大家介绍了使用jQuery Mobile框架如何去设计手机新闻浏览器,其中实现了一个WEB版本的新闻浏览器,在本教程的最后一篇中,将讲解如何将已实现的web版本的新闻 ...

  5. UIView动画学习笔记

    UIView的动画是通过修改控件的属性来达到动画的效果,如:渐变, 移动. 废话不多说,直接上代码: - (void)loadView{ [super loadView]; _leftView = [ ...

  6. 嵌入式 使用udev高效、动态地管理Linux 设备文件

    本文以通俗的方法阐述 udev 及相关术语的概念.udev 的配置文件和规则文件,然后以 Red Hat Enterprise Server 为平台演示一些管理设备文件和查询设备信息的实例.本文会使那 ...

  7. MultiMap

    类关系 ArrayListMultiMap.java Multimap <I> | | AbstractMultimap <A> Serializable <I> ...

  8. Yii 实现MySQL多库和读写分离

    前段时间为SNS产品做了架构设计,在程序框架方面做了不少相关的压力测试,最终选定了YiiFramework,至于为什么没选用公司内部的PHP框架,其实理由很充分,公司的框架虽然是“前辈”们辛苦的积累, ...

  9. Windows执行打开文件命令

    ShellExecute(NULL, "open",  localFile.c_str(),  NULL, NULL, SW_SHOW);          会调用该文件类型关联的 ...

  10. Linux man命令数字含义

    1,用户在shell环境中可以操作的命令或可执行文件   2,系统内核可调用的函数与工具等,即由内核提供的函数. 如open,write之类的(通过这个,可以很方便的查到调用这个函数时需要加什么头文件 ...