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。
收起
输入
第1行:一个数T,表示后面用作输入测试的数的数量。(1 <= T <= 10000)
第2 - T + 1行:每行1个数N(1 <= N <= 10^18)
输出
共T行,每行1个数,输出>= n的最小的只包含因子2 3 5的数。
输入样例
5
1
8
13
35
77
输出样例
2
8
15
36
80
思路:预处理出来所有因子只有2,3,5的数,排序,然后输入进行二分查找
AC代码:
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
#define maxn 1e18+100
ll a[100005];
int num;
ll pd(int x,int y,int z)
{
ll res=1;
for(int i=1;i<=x && res<maxn;i++)
res*=2;
for(int i=1;i<=y && res<maxn;i++)
res*=3;
for(int i=1;i<=z && res<maxn;i++)
res*=5;
if(res>1 && res<maxn)
return res;
return 0;
}
void init()
{
num=0;
for(int i=0;i<=65;i++)
for(int j=0;j<=55;j++)
for(int k=0;k<=55;k++)
{
ll x=pd(i,j,k);
if(x>0)
a[num++]=x;
}
}
int main()
{
int t;
scanf("%d",&t);
init();
sort(a,a+num);
while(t--)
{
ll n;
scanf("%lld",&n);
ll l=0,r=num,mid,ans=0;
while(l<=r)
{
mid=(l+r)/2;
if(a[mid]==n)
{
ans=mid;break;
}
if(a[mid]>n)
r=mid-1;
else
l=mid+1;
}
if(!ans)
ans=l;
printf("%lld\n",a[ans]);
//printf("%lld\n",*lower_bound(a,a+num,n));
}
return 0;
}
还有,也可以把主函数换成:
int main()
{
int t;
scanf("%d",&t);
init();
sort(a,a+num);
while(t--)
{
ll n;
scanf("%lld",&n);
printf("%lld\n",*lower_bound(a,a+num,n)); //此函数作用见下面博客链接
}
return 0;
}
函数用法:https://blog.csdn.net/u011008379/article/details/50725670