1010 只包含因子2 3 5的数
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
先打表,然后直接二分查找lower_bound()
当然也是可以用的。
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
#define endl "\n"
const long long maxn=1e18+100;
const long long Max=196000;
long long a[Max];
long long sum;
void Init(){
memset(a,0,sizeof(a));
sum=0;
for(long long i=1;i<=maxn;i*=2){
for(long long j=1;i*j<maxn;j*=3){
for(long long k=1;i*j*k<maxn;k*=5){
a[sum++]=i*j*k;
}
}
}
sort(a,a+sum);
}
long long Search(long long x){
long long l=0,r=sum,mid;
while(r-l>1){
mid=(l+r)/2;
if(a[mid]>=x){
r=mid;
}else{
l=mid;
}
}
return a[r];
}
int main (){
Init();
int n;
cin>>n;
while(n--){
long long m;
cin>>m;
cout<<Search(m)<<endl;;
}
return 0;
}
/**
int main (){
Init();
int n;
cin>>n;
while(n--){
long long m;
cin>>m;
int pos=lower_bound(a+1,a+sum,m)-a;
cout<<a[pos]<<endl;
//cout<<Search(m)<<endl;;
}
return 0;
}
*/