51nod-1010 只包含因子2 3 5的数

来源:互联网 发布:淘宝网店图片大全 编辑:程序博客网 时间:2024/05/20 21:20
                      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
先打表,再排序
#include <iostream>#include <cstdio>#include <algorithm>using namespace std;typedef long long int ll;const ll INF = 1e18+1e9;ll a[100005];int cnt = 0;int x[] = {2,3,5};void init()  {      ll i, j, k;      for(i = 1; i < INF; i*=2)          for(j = 1; i*j < INF; j*=3)              for(k = 1; k*j*i < INF; k*=5)                  a[cnt++] = i*j*k;  }  int main(){    init();    sort(a, a+cnt);    int t;    scanf("%d", &t);    while(t--){        ll y;        scanf("%I64d", &y);        int ans = lower_bound(a, a+cnt, y) - a;        if(ans == 0) ans++;        printf("%I64d\n", a[ans]);    }    return 0;}


0 0
原创粉丝点击