hdu3199丑数 && hdu1058 humble numbers 小数扩展到大数组成序列

hdu 3199 丑数

题意:将素因子只有p1,p2,p3的数排序,求第n个数是多少

拿小数乘p1,p2,p3不断扩展得到整个序列。比较棒的一个思路是用l1来标记p1应该由序列里第几个数来扩展

用优先权队列的话不好控制重复入队的数

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
//1e18  2^60            开200的时候T ??
typedef long long LL;
LL dp[10000];
int main()
{
    int p1,p2,p3,idx;
    while(scanf("%d%d%d%d",&p1,&p2,&p3,&idx)==4)                //没写while wa了
    {
        dp[0]=1;
        int l1,l2,l3;

        l1=l2=l3=0;
        for(int i=1;i<=idx;++i)
        {
            dp[i]=min(min(dp[l1]*p1,dp[l2]*p2),dp[l3]*p3);
            if(!(dp[i]%p1)) ++l1;
            if(!(dp[i]%p2)) ++l2;
            if(!(dp[i]%p3)) ++l3;
        }
        printf("%lld\n",dp[idx]);

    }


    return 0;
}

hdu 1058 

题意:素因子数只含有2 3 5 7的数为humble number,要求输出第n个humble number

输出比较奇怪

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int dp[5900];
int main()
{
    dp[0]=0;
    dp[1]=1;
    int l1,l2,l3,l4;
    l1=l2=l3=l4=1;
    for(int i=2;i<=5842;++i)
    {
        dp[i]=min(min(dp[l1]*2,dp[l2]*3),min(dp[l3]*5,dp[l4]*7));
        if(!(dp[i]%2)) ++l1;
        if(!(dp[i]%3)) ++l2;
        if(!(dp[i]%5)) ++l3;
        if(!(dp[i]%7)) ++l4;
    }
    int n;
    while(scanf("%d",&n)!=EOF&&n)
    {
        if(n%10==1&&n%100!=11)
            printf("The %dst humble number is %d.\n",n,dp[n]);
        else if(n%10==2&&n%100!=12)
            printf("The %dnd humble number is %d.\n",n,dp[n]);
        else if(n%10==3&&n%100!=13)
            printf("The %drd humble number is %d.\n",n,dp[n]);
        else
            printf("The %dth humble number is %d.\n",n,dp[n]);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值