洛谷——P1748 H数

P1748 H数

题目背景

题目描述

所谓H数,是指只含有2,3,5,7这些质因数的数,如630是H数,而22不是。现在要求输出第n个H数,为了方便起见将H[1]定为1。已知n不超过10000,最后数据在int64范围之内。

输入输出格式

输入格式:

 

一个数n(如题目)

 

输出格式:

 

第n个H数

 

输入输出样例

输入样例#1:  复制
input1:30
input2:1
输出样例#1:  复制
output1:49
output2:1

说明

穷举会爆掉,要用生成法,最好加优化,不然空间复杂度比较大

 

 

12分、、(真的很崩溃、、)

                        #include<queue>
#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 100000
#define LL long long
using namespace std;
int w[4]={2,3,5,7};
LL n,m,nx,sum,ans,a[N];
const long long int maxn=pow(2,64)-1;
priority_queue<LL,vector<LL>,greater<LL> >q;
LL read()
{
    LL x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int main()
{
    n=read(),q.push(1);
    while(!q.empty())
    {
        int x=q.top();q.pop();
        if(x!=a[sum]) sum++,a[sum]=x;
        if(sum>=n) 
        {
            ans=x;
            break;
        }
        for(int i=0;i<4;i++)
        {
            nx=x*w[i];
            if(nx>maxn) break;
            q.push(nx);
        } 
    }
    printf("%lld",ans);
    return  0;
}
                    
12分代码

 

转载于:https://www.cnblogs.com/z360/p/7899885.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值