I Count Two Three HDU – 5878 (打表+二分搜索+输入挂)
It all started several months ago.
We found out the home address of the enlightened agent Icount2three and decided to draw him out.
Millions of missiles were detonated, but some of them failed.
After the event, we analysed the laws of failed attacks.
It’s interesting that the ii-th attacks failed if and only if ii can be rewritten as the form of 2a3b5c7d2a3b5c7d which a,b,c,da,b,c,d are non-negative integers.
At recent dinner parties, we call the integers with the form 2a3b5c7d2a3b5c7d “I Count Two Three Numbers”.
A related board game with a given positive integer nn from one agent, asks all participants the smallest “I Count Two Three Number” no smaller than nn.
InputThe first line of input contains an integer t (1≤t≤500000)t (1≤t≤500000), the number of test cases. tt test cases follow. Each test case provides one integer n (1≤n≤109)n (1≤n≤109).OutputFor each test case, output one line with only one integer corresponding to the shortest “I Count Two Three Number” no smaller than nn.Sample Input
10 1 11 13 123 1234 12345 123456 1234567 12345678 123456789
Sample Output
1 12 14 125 1250 12348 123480 1234800 12348000 123480000
简单思考可以得出,符合要求的数字不是特别多,于是采用打表的方法
这道题先枚举a,b,c,d的值,然后将其存入数组内,之后进行排序。
然后根据输入二分搜索确定数字。
但是还是会超时,换上了输入挂之后就AC了.
#include<iostream>
#include<algorithm>
using namespace std;
inline bool scan_d(int &num)
{
char in;bool IsN=false;
in=getchar();
if(in==EOF) return false;
while(in!='-'&&(in<'0'||in>'9')) in=getchar();
if(in=='-'){ IsN=true;num=0;}
else num=in-'0';
while(in=getchar(),in>='0'&&in<='9'){
num*=10,num+=in-'0';
}
if(IsN) num=-num;
return true;
}
long long sum(int a,int b,int c,int d){
long long x=1;
for(int i=0;i<a;i++){
x*=2;
}
for(int i=0;i<b;i++){
x*=3;
}
for(int i=0;i<c;i++){
x*=5;
}
for(int i=0;i<d;i++){
x*=7;
}
return x;
}
long long ax[8000] ;
int main(){
int s=0;
for(int a=0;a<30;a++){
for(int b=0;b<20;b++){
for(int c=0;c<15;c++){
for(int d=0;d<13;d++){
long long x=sum(a,b,c,d);
if(x<10000000000&&x>0){
ax[s++]=x;
}else{
break;
}
}
}
}
}
sort(ax,ax+s);
// cout<<s<<endl;
int t;
scan_d(t);
int xxx;
for(int i=0;i<t;i++){
scan_d(xxx);
int L=0,R=s;
while(L<R){
int mid=(L+R)/2;
if(ax[mid]>=xxx){
R=mid;
}else{
L=mid+1;
}
}
printf("%d\n",ax[L]);
}
return 0;
}