1. 程式人生 > >51nod 1010 只包含因子2 3 5的數(打表+排序+二分)

51nod 1010 只包含因子2 3 5的數(打表+排序+二分)

F12 alt bbb art 打表 names syn tdi iostream

1010 只包含因子2 3 5的數技術分享 基準時間限制:1 秒 空間限制:131072 KB 分值: 10 難度:2級算法題 技術分享 收藏 技術分享 關註 技術分享 取消關註 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

技術分享
 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 typedef long long ll;
 5 const ll N=1e18+1000;
 6 const ll maxn=1e6+10;
 7 int T;
 8 ll a[maxn];
 9 int d;
10 void init()
11 {
12
d=0; 13 for(ll i=1;i<N;i*=2) 14 for(ll j=1;j*i<N;j*=3) 15 for(ll k=1;i*j*k<N;k*=5) 16 a[d++]=i*j*k; 17 a[0]=-1; 18 sort(a,a+d); 19 } 20 int main() 21 { 22 ios::sync_with_stdio(false); 23 init(); 24 cin>>T; 25 while(T--) 26 { 27
ll tmp; 28 cin>>tmp; 29 ll pos=lower_bound(a,a+d,tmp)-a; 30 cout<<a[pos]<<endl; 31 } 32 return 0; 33 }
View Code

 

51nod 1010 只包含因子2 3 5的數(打表+排序+二分)