题目描述


基准时间限制: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


解题思路


/* 这道题的大题思路就是打表+二分查找 但是关键是这道题的数据量1e18,看着很 吓人,那么既然要打表,数组要开多大才比较 好了,我在网上找了很多大神的做法。觉得比较 好的一个: 包含2.3.5的因子可以写成2^a*3^b*5^c,a,b,c 代表系数,又因为2^60 > 10^18,所以数组开成 60*60*60,坑定是够的。 */

代码


import java.util.Scanner;
public class Main{
    static int index = 0;
    static int k = 60*60*60;
    static long[] table = new long[k];
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        datable();
        while(T-- > 0){
            long tem = sc.nextLong();
            int left = 1;
            int right = index;
            int mid = 0;
            while(left < right ){
                mid = (right-left)/2 + left;
                if(tem <= table[mid] )
                    right = mid;
                else
                    left = mid+1;
            }
            System.out.println(table[left]);
        }
    }
    //打表
    public static void datable(){
        table[0] = 1;
        int x2 =0;
        int x3 =0;
        int x5 =0;
        while(table[index] < 1e18){
        long min = Math.min(table[x2]*2, Math.min(table[x3]*3, table[x5]*5));
        table[++index] = min;
        if(min == table[x2]*2) x2++;
        if(min == table[x3]*3) x3++;
        if(min == table[x5]*5) x5++;
      }
    }
}