我数23 (丑数变题)

题目描述
给你一个数字n,找出大于等于n的最小的,质因数只有2,3,5,7的数。

输入
第一行输入一个数字T,表示有T组测试数据,1 <= T <= 500000

之后T行,每一行有一个数字n,0 <= n <= 109

输出
对每个样例,皆输出一个数字表示答案。

样例输入
5
1
11
111
1111
11111
样例输出
210
210
210
1260
11340

题解:用优先队列打标,然后二分查找,注意使用scanf和printf不然会时间超限....别问我为什么,WA的经验....

#include<iostream>
#include<cstdio>
#include<string>
#include<sstream>
#include<queue>
#include<set>
#include<vector>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int coeff[4] = {2,3,5,7};
long long  b[2000]={0};
typedef long long LL;
int main(){
	int T;
	priority_queue<LL,vector<LL>,greater<LL> > pq;
	set<LL>s;
	pq.push(210);
	s.insert(210);
	int k=1;
	b[0]=210;
	for(int i=1;;i++){
		LL x=pq.top();pq.pop();
		if(i==1700){
			break;
		}
		for(int j=0;j<4;j++){
			LL x2=x*coeff[j];
			if(!s.count(x2)){
				b[k++]=x2;
//				cout<<b[k-1]<<" ";
				s.insert(x2);
				pq.push(x2);
			}
		}
	}
	sort(b,b+k);
//	for(int i=0;i<k;i++){
//		cout<<b[i]<<" ";
//	}
	scanf("%d",&T);
	while(T--){
			long long  x;
			scanf("%lld",&x);
			int  p=lower_bound(b,b+k,x)-b;
			printf("%lld\n",b[p]);
			
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41991981/article/details/81878553