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. 51nod1158 全是1的最大子矩阵

    跟最大子矩阵差不多O(n3)扫一下.有更优写法?挖坑! #include<cstdio> #include<cstring> #include<cctype> #i ...

  2. JS Array常用方法indexOf/filter/forEach/map/reduce详解

    Array共有九个方法   Array.prototype.indexOf Array.prototype.lastIndexOf Array.prototype.every Array.protot ...

  3. 【经验】Windows7、8、8.1 MSI安装错误Error Code 2502 & 2503 解决方法

    [因] 今天升级TortoiseSVN到1.8.8,出现问题:Error Code 2502 & 2503,一直不能安装成功. 上网一搜,国内没找到好的解决方法,在一个外文网上找到了方案,原链 ...

  4. xcode快捷键的使用

    1.统计Xcode项目代码行数 1   打开终端. 2  用ls和cd进到你项目的路径. 3   输入下面的指令: grep -r "\n" classes | wc -l (cl ...

  5. pipe()管道最基本的IPC机制

    <h4>进程间通信 fork pipe pie_t 等用法(管道机制 通信)</h4>每个进程各自有不同的用户地址空间,任何一个进程的全局变量在另一个进程中都看不到,所以进程之 ...

  6. UVa 11464 Even Parity 偶数矩阵

    给你一个 n * n 的 01 矩阵,现在你的任务是将这个矩阵中尽量少的 0 转化为 1 ,使得每个数的上下左右四个相邻的数加起来是偶数.求最少的转化个数. 首先,n 的规模并不大,最大只有15.但是 ...

  7. 操作系统——CPU、计算机的构成

    CPU主要由什么构成? CPU包括运算逻辑部件.寄存器部件和控制部件等. 逻辑部件:可以执行点或浮点算术远算操作.移位操作以及逻辑操作,也可以执行地址运算和转换. 寄存器部件:存储程序.数据和各种信号 ...

  8. 【转】Linux高级字符设备之Poll操作

    原文网址:http://www.cnblogs.com/geneil/archive/2011/12/04/2275559.html 在用户程序中,select()和poll()也是与设备阻塞与非阻塞 ...

  9. yeoman bower grunt等安装

    grunt-beginner前端自动化工具:http://www.imooc.com/learn/30 grunt的安装 官方站点:http://gruntjs.com/ 安装指令: sudo npm ...

  10. Arduino 电平转换 升压 OUTPUT与9V/12V元件通信

    原因 网络上有不少怎么让Arduino的5V电平转换成3.3V电平,从而和工作在3.3V下的芯片相互沟通的教程.但是如果想驱动高于5V电压的芯片,就找不到教程了.因此今天我来介绍一种方式,能让Ardu ...