傳送門
K的因子中只包括2 3 5。滿足條件的前10個數(shù)是:2,3,4,5,6,8,9,10,12,15。
所有這樣的K組成了1個序列S,現(xiàn)在給出1個數(shù)n,求S中 >= 給定數(shù)的最小的數(shù)。
例如:n = 13,S中 >= 13的最小的數(shù)是15,所以輸出15。
Input
第1行:1個數(shù)T,表示后面用作輸入測試的數(shù)的數(shù)量。(1 <= T <= 10000)
第2 - T + 1行:每行1個數(shù)N(1 <= N <= 10^18)
Output
共T行,每行1個數(shù),輸出>= n的最小的只包括因子2 3 5的數(shù)。
Input示例
5
1
8
13
35
77
Output示例
2
8
15
36
80
解題思路:
就是首先將所有的只含有2 3 5因子的數(shù)打1個表保存在1個數(shù)組里,然后2分查找第1個>=數(shù)組里的數(shù),輸出就好了。
上代碼:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const LL INF = 1e18+100;///不能太小了
const int MAXN = 70*70*70;
LL a[MAXN];
int cnt = 0;
void Init()
{
cnt = 0;
for(LL i=1; i<INF; i*=2)///(注意i,j,k是LL的)
for(LL j=1; j*i<INF; j*=3)
for(LL k=1; i*j*k<INF; k*=5)
a[cnt++] = i*j*k;
}
int main()
{
Init();
sort(a, a+cnt);
int T;
cin>>T;
while(T--)
{
LL n;
scanf("%lld",&n);
printf("%lld\n",a[lower_bound(a+1,a+cnt+1,n)-a]);
}
return 0;
}