(DP) poj 1338

Ugly Numbers

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 21453   Accepted: 9586

Description

Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, ... 
shows the first 10 ugly numbers. By convention, 1 is included. 
Given the integer n,write a program to find and print the n‘th ugly number.

Input

Each line of the input contains a postisive integer n (n <= 1500).Input is terminated by a line with n=0.

Output

For each line, output the n’th ugly number .:Don’t deal with the line with n=0.

Sample Input

1
2
9
0

Sample Output

1
2
10

Source

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
int n,ans[1505];
int main()
{
    ans[1]=1;
    int p1=1,p2=1,p3=1;
    for(int i=2;i<=1500;i++)
    {
        ans[i]=min(ans[p1]*2,min(ans[p2]*3,ans[p3]*5));
        if(ans[i]==ans[p1]*2) p1++;
        if(ans[i]==ans[p2]*3) p2++;
        if(ans[i]==ans[p3]*5) p3++;
    }
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
        printf("%d\n",ans[n]);
    }
    return 0;
}

  

时间: 2024-08-25 22:52:48

(DP) poj 1338的相关文章

[dp] poj 1015 Jury Compromise

题目链接: http://poj.org/problem?id=1015 Jury Compromise Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24438   Accepted: 6352   Special Judge Description In Frobnia, a far-away country, the verdicts in court trials are determined by a jury

递推(三):POJ中的三道递推例题POJ 1664、POJ 2247和POJ 1338

[例9]放苹果(POJ 1664) Description 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法. Input 第一行是测试数据的数目t(0 <= t <= 20).以下每行均包含二个整数M和N,以空格分开.1<=M,N<=10. Output 对输入的每组数据M和N,用一行输出相应的K. Sample Input 1 7 3 Sample Output 8 (1)编程思路. 设f[m]

[kuangbin 基础dp][POJ 1015] Jury Compromise(dp)

[kuangbin 基础dp][POJ 1015] Jury Compromise 题目 In Frobnia, a far-away country, the verdicts in court trials are determined by a jury consisting of members of the general public. Every time a trial is set to begin, a jury has to be selected, which is do

POJ 1338 Ugly Numbers dp

 Ugly Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21867   Accepted: 9767 Description Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, ... shows the first 10 ugly

hdu 1058 Humble Numbers || poj 1338(dp)

两题都是一样的题目 只是hdu 1058 多了个7 题意:求一个每个数因子仅含2 3 5 7 的 序列 问 第n个数是几 思路: ans[i]=min(min(ans[n2]*2,ans[n3]*3),min(ans[n5]*5,ans[n7]*7)); if(ans[i]==ans[n2]*2) n2++; if(ans[i]==ans[n3]*3) n3++; if(ans[i]==ans[n5]*5) n5++; if(ans[i]==ans[n7]*7) n7++; hdu 1058 #

Ugly Numbers POJ 1338

题目链接: http://poj.org/problem?id=1338 题意 :只有素数因子 2, 3, 5 的数字才成为丑陋数字,现给出一个数字 N , 求在丑陋数字的序列里, 第N个丑陋数字是多少. 一开始想的简单粗暴, 搞个循环去存储丑陋数字了..到最后发现输入N=1500的时候, 压根什么都出不来, 已哭死.. 比赛结束后百度了一下, 才发现自己真的是单细胞生物, 找不到这个规律... ********* 规律如下:    这个丑陋数字集合是通过集合里的每一个数 × 2,× 3,× 5

(状态压缩DP) poj 2441

Arrange the Bulls Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 3709   Accepted: 1422 Description Farmer Johnson's Bulls love playing basketball very much. But none of them would like to play basketball with the other bulls because the

树形dp poj 2342

题目链接:poj 2342 题目大意:某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知每个人的活跃指数和上司关系(当然不可能存在环),求邀请哪些人(多少人)来能使得晚会的总活跃指数最大. 思路:既然是树形dp,自然先建立一颗树了,这里用的是邻接表(L<-K).找根的时候利用flag数组来标记所有儿子节点,那么没有标记的自然是根节点了.树形dp从叶子节点开始dp,所以深搜到叶子节点,然后不断回溯给父节点.在处理儿子节点时,父节点不参加

递推DP POJ 1163 The Triangle

题目传送门 1 /* 2 数塔 3 自底向上 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <cstring> 8 #include <string> 9 #include <algorithm> 10 #include <cmath> 11 using namespace std; 12 13 const int MAXN = 100 + 10; 14 const