您的位置:首页 > 其它

HDOJ 5878 I Count Two Three 【2016青岛网赛】暴力打表+二分枚举

2016-09-19 23:48 453 查看
打表其实还是很明显的

因为满足2^a*3^b*5^c*7^d这个样子的,在【1,1e9】中其实是没有多少的

那么,我们先把2,3,5,7的次方数组打出来,然后枚举a,b,c,d

然后把所有满足条件的数放到一个数组中,然后查询搞一发就可以了

主要还是在枚举和二分细节上注意别出错就好

#include<bits/stdc++.h>
#include<algorithm>
using namespace std;

#define LL __int64
const LL maxn=1E9;
LL x[1000050];
LL pow2[100],pow3[100],pow5[100],pow7[100];
int tot=0;

void getnum(){
pow2[0]=pow3[0]=pow5[0]=pow7[0]=1;
for(int i=1;;i++){
pow2[i]=2*pow2[i-1];
//cout<<pow2[i]<<endl;
if (pow2[i]>maxn) break;
}
for(int i=1;;i++){
pow3[i]=3*pow3[i-1];
//cout<<pow3[i]<<endl;
if (pow3[i]>maxn) break;
}
for(int i=1;;i++){
pow5[i]=5*pow5[i-1];
//cout<<pow5[i]<<endl;
if (pow5[i]>maxn) break;
}
for(int i=1;;i++){
pow7[i]=7*pow7[i-1];
//cout<<pow7[i]<<endl;
if (pow7[i]>maxn) break;
}
tot=0;
for(int a=0;a<=30;a++)
for(int b=0;b<=19;b++)
if (pow2[a]*pow3[b]<=maxn)
for(int c=0;c<=13;c++)
if (pow2[a]*pow3[b]*pow5[c]<=maxn)
for(int d=0;d<=11;d++)
if (pow2[a]*pow3[b]*pow5[c]*pow7[d]<=maxn)
x[++tot]=pow2[a]*pow3[b]*pow5[c]*pow7[d];
sort(x+1,x+1+tot);
}

int main(){
//freopen("input.txt","r",stdin);
getnum();
//for(int i=1;i<=tot;i++)
// cout<<x[i]<<endl;
int t;
LL num;
while(scanf("%d",&t)!=EOF){
for(int Case=1;Case<=t;Case++){
scanf("%I64d",&num);
//cout<<num<<endl;
int L=1,R=tot,ans,mid;
while(L<=R){
mid=(L+R)>>1;
if (x[mid]>=num){
ans=mid;
R=mid-1;
}
else L=mid+1;
}
printf("%I64d\n",x[ans]);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: