51NOD 1010 只包含因子2 3 5的数二分 + 预处理

传送门

K的因子中只包括2 3 5。满足条件的前10个数是:2,3,4,5,6,8,9,10,12,15。
所有这样的K组成了1个序列S,现在给出1个数n,求S中 >= 给定数的最小的数。
例如:n = 13,S中 >= 13的最小的数是15,所以输出15。
Input
第1行: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
解题思路:
就是首先将所有的只含有2 3 5因子的数打1个表保存在1个数组里,然后2分查找第1个>=数组里的数,输出就好了。
代码

#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; }

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...