51Nod1010 只包含因子2 3 5的数 (二分

来源:互联网 发布:淘宝关键词挖掘 编辑:程序博客网 时间:2024/05/29 15:02

1010 只包含因子2 3 5的数

Description

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的数。

Sample Input

5
1
8
13
35
77

Sample Output

2
8
15
36
80

题意

中文题

题解:

打表+二分 这里我是自己写的二分 还有一种是STL中的lower_bound实现查找的QAQ

AC代码

#include <cstdio>#include <cstring>#include <cmath>#include <queue>#include <stack>#include <map>#include <set>#include <iostream>#include <vector>#include <algorithm>using namespace std;#define ll long longconst int mod = 1e9+7;const ll N = 1e18+100;  //当时加了个10一直wa 2*5*3>10ll arr[11000];int ans = 0;void init(){    for(ll i = 1; i < N; i*=2) {        for(ll j = 1; j*i < N; j*=3) {            for(ll k = 1; k*j*i < N; k*=5) {                arr[ans++] = i*j*k;            }        }     }    }ll find(ll x){    ll l = 1;    ll h = ans;    while(l<h) {        int mid = (l+h) >> 1;        if(arr[mid]==x) return arr[mid];        else if(arr[mid] < x) l = mid + 1;        else h = mid ;    }return arr[h]; } int main(){    init();    sort(arr,arr+ans);    int T;      scanf("%d",&T);    while(T--) {        ll n;        scanf("%lld",&n);        printf("%lld\n",find(n));    } return 0;}
1 0
原创粉丝点击