Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
SubmitStatus
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 100Sample 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位时退出
(当然,并没有实现,不确定对错)
##三
hdu1058