首页 > 其他 > 详细

洛谷——P1748 H数

时间:2017-11-26 20:01:56      阅读:293      评论:0      收藏:0      [点我收藏+]

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分代码

 

洛谷——P1748 H数

原文:http://www.cnblogs.com/z360/p/7899885.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!