51Nod 1010 只包含因子2 3 5的数 ——————思维,数学

版权声明:听说这里是写版权声明的 https://blog.csdn.net/Hpuer_Random/article/details/82021108

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示例
5
1
8
13
35
77

Output示例
2
8
15
36
80


K的因子中之包含 2,3,5;
所以 K 可以写成

K = 2 x   3 y   5 z

其中 x 0   ,   y     0   ,   z 0  

所以打表,排序,二分就好了


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MAXN=1e18+9999;
ll a[1055000];//刚开始数组开小了
int main()
{
    int cnt=0;
    for(ll i=1;i<=MAXN;i*=2)
        for(ll j=1;j*i<=MAXN;j*=3)
            for(ll k=1;k*j*i<=MAXN;k*=5)
                a[cnt++]=i*j*k;
//    printf("%d\n",cnt);
    sort(a,a+cnt);
//    for(int i=0;i<100;i++)
//        printf("a[%3d]:%4lld\n",i,a[i]);
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ll n;
        scanf("%lld",&n);

        printf("%lld\n",a[lower_bound(a+1,a+cnt+1,n)-a]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Hpuer_Random/article/details/82021108