您的位置:首页 > 其它

2016 ACM/ICPC 青岛网赛 1001(预处理+二分)

2017-09-10 21:40 211 查看
I Count Two Three

题意:求能用2^a*3^b*5^c*7^d表示的大于等于n的值,其中a,b,c,d均为非负整数。

我们可以先把能用2^a*3^b*5^c*7^d表示的数打表,,然后二分即可。n的最大值为10^9,估算一下数组开到6000即可。

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

const int max_n=6000;
const int inf=1000000000;

int b[max_n]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int p[4]={2, 3, 5, 7};
int n;

void init()
{
int crt;
for (int i=10;i<max_n;i++)
{
crt=i-1;
b[i]=inf;
for (int j=0;j<4;j++)
{
while(b[crt-1]*p[j]>b[i-1])   crt--;
b[i]=min(b[i],b[crt]*p[j]);
}
}
}

int main()
{
init();

int t;
cin >> t;

while (t--)
{
scanf("%d",&n);
int res=*lower_bound(b,b+max_n,n);
printf("%d\n",res);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐