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

来源:互联网 发布:反转链表 java 编辑:程序博客网 时间:2024/06/01 09:12
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示例
518133577
Output示例
28153680
李陶冶 (题目提供者)
题解: 将1e18+999范围内所有符合题意的数提前处理出来,这里要超过1e18的,因为题中说了n会等于1e18的。好像大概1万个吧,然后排序一下,对于给定的一个数,就是二分找符合条件的最小值问题了,经典二分。

蠢了,竟然不会预处理了(其实主要是一直以为打表会打不下。。)

代码:
#include<iostream>#include<cstring>#include<math.h>#include<stdlib.h>#include<cstring>#include<cstdio>#include<utility>#include<algorithm>#include<map>#include<stack>#include<queue>using namespace std;typedef long long ll;const int maxn = 1e6+5;const int mod = 1e9+7;const int Hash = 10000;const int INF = 1<<30;const ll llINF = 1e18+999;ll n;ll ans[maxn];int cnt;//预处理void init( ){    cnt = 0;    for(ll i=1; i<=llINF; i*=2)        for(ll j=1; i*j<=llINF; j*=3)            for(ll k=1; i*j*k<=llINF; k*=5)                ans[cnt++] += i*j*k;    sort(ans, ans+cnt);//记得要排序的}int main( ){    init( );    int T;    scanf("%d", &T);    while(T--)    {        scanf("%lld", &n);        ll low = 1, high = cnt;        //二分查就好了,这里我写的是闭区间二分        while(low < high)        {            int mid = (low+high)>>1;            if(ans[mid]<n)                low = mid+1;            else                high = mid;        }        cout<<ans[high]<<endl;    }}


原创粉丝点击