您的位置:首页 > 其它

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

2017-02-28 21:10 225 查看
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刚开始做时也想到了用打表,感觉一定会超时的毕竟数据很大
10^18,其实长整型的最大值是2^64-1,根据这个我们就知道了是不可能
超时的,所以直接打表,sort加二分就行了
可以看到数组我只开到65*65*65 这个是肯定满足条件的
还要多谢学长的指点 

#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
const LL maxn = 1e18+1000;
LL num[65*65*65];
int index;
void Init()
{
LL i,j,k;
index=0;
for(i=1;i<maxn;i*=2)
for(j=1;j*i<maxn;j*=3)
for(k=1;i*j*k<maxn;k*=5)
num[index++]=i*j*k;
}
int main()
{
Init();
sort(num,num+index);
int t;
scanf("%d",&t);
while(t--)
{
LL n;
scanf("%lld",&n);
int pos=lower_bound(num+1,num+index,n)-num;
printf("%lld\n",num[pos]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: