[2016ICPC 青岛网络预选赛] HDU 5878 筛表

来源:互联网 发布:硕鼠mac youtube 编辑:程序博客网 时间:2024/05/17 01:37

题意

找出大于n(<=1e9)的最小的只有2,3,5,7为质因数的数,可以不全有,但不能有别的质因数。

思路

N挺大不过2^30次方,所以所求的数的质因数分解,2357的个数都不会超过30。那么我们先打表找出2357构成的2*1e9以内的所有数,然后排序。没读入一个n找大于等于n的第一个数。

AC代码 C++

#include <stdio.h>#include <algorithm>using namespace std;#define MX 100005#define LIMIT 2000000000long long ar[MX];int main(){    int t, n, i, tl = 0;    long long ans2 = 1, ans3, ans5, ans7;    for (i = 0; i<30 && ans2<LIMIT; i++, ans2 *= 2)        for (t = 0, ans3 = ans2; t<30 && ans3<LIMIT; t++, ans3 *= 3)            for (ans5 = ans3; ans5<LIMIT; ans5 *= 5)                for (ans7 = ans5; ans7<LIMIT; ans7 *= 7)                    ar[tl++] = ans7;    sort(ar, ar + tl);    scanf("%d", &t);    while (t-- && scanf("%d", &n) > 0)        printf("%I64d\n", *lower_bound(ar, ar + tl, n));    return 0;}
0 0