51nod 1010 只包含因子2 3 5的数 打表二分

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
思路:dfs打表。。。然后排序,二分出答案
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5 typedef long long int ll;
 6 const ll INF = 1e18+1e9;
 7 ll a[100005];
 8 int cnt = 0;
 9 int x[] = {2,3,5};
10 void dfs(int pos, ll num){
11     if(pos ==  3) {
12         a[cnt++] = num;
13         return;
14     }
15     dfs(pos+1, num);
16     for(int i = 1; i <= 64; i++){
17         if(num*x[pos]> INF) break;
18         dfs(pos+1, num *= x[pos]);
19     }
20 }
21 int main()
22 {
23     dfs(0,1);
24     sort(a, a+cnt);
25     int t;
26     scanf("%d", &t);
27     while(t--){
28         ll y;
29         scanf("%I64d", &y);
30         int ans = lower_bound(a, a+cnt, y) - a;
31         if(ans == 0) ans++;
32         printf("%I64d\n", a[ans]);
33     }
34 
35     return 0;
36 }

 

 

posted on 2016-09-20 18:39  disppr  阅读(283)  评论(0编辑  收藏  举报