POJ1338Ugly Numbers(DP)
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)的更多相关文章
- Gym 100703G---Game of numbers(DP)
题目链接 http://vjudge.net/contest/132391#problem/G Description standard input/outputStatements — It' s ...
- URAL 1586 Threeprime Numbers(DP)
题目链接 题意 : 定义Threeprime为它的任意连续3位上的数字,都构成一个3位的质数. 求对于一个n位数,存在多少个Threeprime数. 思路 : 记录[100, 999]范围内所有素数( ...
- Codeforces 403D: Beautiful Pairs of Numbers(DP)
题意:转换模型之后,就是1~n个数中选k个,放到一个容量为n的背包中,这个背包还特别神奇,相同的物品摆放的位置不同时,算不同的放法(想象背包空间就是一个长度为n的数组,然后容量为1的物体放一个格子,容 ...
- 【gym102394B】Binary Numbers(DP)
题意:From https://blog.csdn.net/m0_37809890/article/details/102886956 思路: 可以发现转移就是右上角的一个区间前缀和 std只要开1倍 ...
- 【CF55D】Beautiful numbers(动态规划)
[CF55D]Beautiful numbers(动态规划) 题面 洛谷 CF 题解 数位\(dp\) 如果当前数能够被它所有数位整除,意味着它能够被所有数位的\(lcm\)整除. 所以\(dp\)的 ...
- LightOJ 1033 Generating Palindromes(dp)
LightOJ 1033 Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- lightOJ 1047 Neighbor House (DP)
lightOJ 1047 Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...
- UVA11125 - Arrange Some Marbles(dp)
UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...
- Humble Numbers(hdu1058)
Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
随机推荐
- 【英语】Bingo口语笔记(6) - 表示“迷茫”
- 【英语】Bingo口语笔记(53) - 口语中不可望文生义的词语
- gridview自定义表头
gridview为我们提供了丰富的接口,用于满足自定义需求. 通常asp:gridview会根据绑定的列Columns自动生成表头,展现在前台元素. 序号 类别 有时候需要复杂一些的表头. 序号 类别 ...
- 【解题报告】PKU 2318 TOYS AND PKU 2398 Toy Storage
题目连接: http://poj.org/problem?id=2318 http://poj.org/problem?id=2398 两题类似的题目,2398是2318的升级版. 题目大概是 ...
- Heritrix源码分析(五) 如何让Heritrix在Ecplise等IDE下编程启动(转)
本博客属原创文章,欢迎转载!转载请务必注明出处:http://guoyunsky.iteye.com/blog/642550 本博客已迁移到本人独立博客: http://www.yun5u. ...
- hbase+hive应用场景
一.Hive应用场景本文主要讲述使用 Hive 的实践,业务不是关键,简要介绍业务场景,本次的任务是对搜索日志数据进行统计分析.集团搜索刚上线不久,日志量并不大 .这些日志分布在 5 台前端机,按小时 ...
- MySQL基础之第15章 MySQL用户管理
15.2.账户管理 15.2.1.登录和退出MySQL服务器 mysql –hhostname|hostIP –P port –u username –p[password] databaseName ...
- [Everyday Mathematics]20150223
是否存在 $3\times 3$ 阶实方阵 $A$ 使得 $\tr A=0$ 且 $A^2+A^T=I$?
- java多态中哪些成员具备多态特性
在多态的学习中,当子类继承父类时,子类中的变量哪些具备多态特性,哪些不具备多特特性. 代码: class Father{ public static int x=10; public int y=11 ...
- Effective java笔记7--线程
一.对可共享数据的同步访问 synchronized关键字可以保证在同一时刻,只有一个线程在执行一条语句,或者一段代码块.正确地使用同步可以保证其他任何方法都不会看到对象处于不一致的状态中,还能保证通 ...