HDU 3199 Hamming Problem 丑数

Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit

Status
Description
For each three prime numbers p1, p2 and p3, let’s define Hamming sequence Hi(p1, p2, p3), i=1, … as containing in increasing order all the natural numbers whose only prime divisors are p1, p2 or p3.

For example, H(2, 3, 5) = 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 25, 27, …

So H5(2, 3, 5)=6.

Input
In the single line of input file there are space-separated integers p1 p2 p3 i.

Output
The output file must contain the single integer - Hi(p1, p2, p3). All numbers in input and output are less than 10^18.

Sample Input
7 13 19 100

Sample Output
26590291

题意分析

  • 给定三个素数p1,p2,p3;
  • H(p1,p2,p3)为连续递增的自然数,且其质因数的集合只能属于{p1,p2,p3};
  • 求第n个在H()这个序列里的数

    解题思路

  • 考虑的范围,一定不是每个数尝试分解质因数

  • 数学定理:任何一个合数都可以写成几个质数相乘的形式。其中每个质数都是这个合数的因数,叫做这个合数的分解质因数。分解质因数只针对合数。(自行百度);
  • 分类:1+质数+合数=全部自然数:不包括1;质因数分解只有1和其本身(也就是说H序列里的质数只有p1,p2,p3);剩下的都是合数(都能分解成几个质数)反过来想就是由(p1,p2,p3)中任意n个组合相乘(这里描述不太严谨,看代码)

    代码

#include<iostream>
#include<cstdio>
#include<set>
#include<queue>
#include<vector>
using namespace std;
long long p[3];
int n;
int main(){

    while(  cin>>p[0]>>p[1]>>p[2]>>n){

        priority_queue<long long,vector<long long>,greater<long long> > Q;
        set<long long> S;
        Q.push(1);
        S.insert(1);
        for(int i=1;;i++){
            long long x=Q.top();Q.pop();
            if(i==n+1){
                cout<<x<<endl;
                break;
            }
            for(int j=0;j<3;j++){
                long long t=x*p[j];
                if(!S.count(t)){
                    Q.push(t);
                    S.insert(t);
                }
            }
        }
    }

    return 0;
}

DFS实现

ans=1;
dfs(ans)
for i=0 to 2 dfs(ans*pi);
保存ans序列并排序去重,当ans大于18位时退出
(当然,并没有实现,不确定对错)
##三

号称dp的解法

hdu1058

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值