51Nod 1010 只包含因子2 3 5的数 Label:None

K的因子中只包含2 3 5。满足条件的前10个数是:2,3,4,5,6,8,9,10,12,15。
所有这样的K组成了一个序列S,现在给出一个数n,求S中 >= 给定数的最小的数。
例如:n = 13,S中 >= 13的最小的数是15,所以输出15。
 
Input
第1行:一个数T,表示后面用作输入测试的数的数量。(1 <= T <= 10000)
第2 - T + 1行:每行1个数N(1 <= N <= 10^18)
Output
共T行,每行1个数,输出>= n的最小的只包含因子2 3 5的数。
Input示例
5
1
8
13
35
77
Output示例
2
8
15
36
80

代码

#include<iostream>  
#include<cstdlib>  
#include<cstring>  
#include<cstdio>  
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#define ll long long
#define INF 1500000000000000000
using namespace std;  

struct cmp{bool operator()(ll a,ll b){return a>b;}};
priority_queue<ll,vector<ll>,cmp> q;
map<ll,int> m;

ll a[1000005],M,p;

void init_(){
    q.push(1);
    while(q.top()<INF){
        ll x=q.top();q.pop();
        a[p++]=x;
        if(!m.count(2*x)) {q.push(2*x);m[2*x]=1;}
        if(!m.count(3*x)) {q.push(3*x);m[3*x]=1;}
        if(!m.count(5*x)) {q.push(5*x);m[5*x]=1;}
    }
    
    //cout<<p<<endl;
    scanf("%d",&M);
}

void work(){
    while(M--){
        ll x;
        scanf("%lld",&x);
        
        printf("%lld
",*lower_bound(a+1,a+p+1,x));
    }
}

int main(){  
//    freopen("01.in","r",stdin);
    
    init_();
    work();
    
    fclose(stdin);fclose(stdout);return 0;
}

  

做一个优先队列,用已经有的数生成更大的数

map去重,lower_bound二分加速

INF少打了一个0,不开心

时间还算可以吧,我觉得主要是询问的时间,因为 计数器菌 表示这样的数在数据范围内只有1w+

原文地址:https://www.cnblogs.com/radiumlrb/p/6064290.html