http://www.lydsy.com/JudgeOnline/problem.php?id=4029
贪心。
枚举有多少个后导0(不妨枚举到有k个后导0),找到第一个大于等于L的$10^k$的倍数,和第一个大于等于L的$5*10^k$的奇数次倍数,只有这2个才有可能成为答案。
然后从中选出最优即可。
#include<cstdio> #include<cstdlib> #include<iostream> #include<fstream> #include<algorithm> #include<cstring> #include<string> #include<cmath> #include<queue> #include<stack> #include<map> #include<utility> #include<set> #include<bitset> #include<vector> #include<functional> #include<deque> #include<cctype> #include<climits> #include<complex> //#include<bits/stdc++.h>适用于CF,UOJ,但不适用于poj using namespace std; typedef long long LL; typedef double DB; typedef pair<int,int> PII; typedef complex<DB> CP; #define mmst(a,v) memset(a,v,sizeof(a)) #define mmcy(a,b) memcpy(a,b,sizeof(a)) #define fill(a,l,r,v) fill(a+l,a+r+1,v) #define re(i,a,b) for(i=(a);i<=(b);i++) #define red(i,a,b) for(i=(a);i>=(b);i--) #define ire(i,x) for(typedef(x.begin()) i=x.begin();i!=x.end();i++) #define fi first #define se second #define m_p(a,b) make_pair(a,b) #define p_b(a) push_back(a) #define SF scanf #define PF printf #define two(k) (1<<(k)) template<class T>inline T sqr(T x){return x*x;} template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;} template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;} const DB EPS=1e-9; inline int sgn(DB x){if(abs(x)<EPS)return 0;return(x>0)?1:-1;} const DB Pi=acos(-1.0); inline int gint() { int res=0;bool neg=0;char z; for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar()); if(z==EOF)return 0; if(z=='-'){neg=1;z=getchar();} for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar()); return (neg)?-res:res; } inline LL gll() { LL res=0;bool neg=0;char z; for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar()); if(z==EOF)return 0; if(z=='-'){neg=1;z=getchar();} for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar()); return (neg)?-res:res; } const int INF=0x3f3f3f3f; LL A,B,ans; int x; inline int col(LL v) { int res=0; while(v%10==0)v/=10; if(v%10==5)res--; while(v!=0)v/=10,res+=2; return res; } inline void DFS(LL div) { int C=((A-1)/div+1)*div,dc=col(C); int D=(((A-1)/(5*div)+1)&1) ? ((A-1)/(5*div)+1)*(5*div) : ((A-1)/(5*div)+1+1)*(5*div),dd=col(D); if(C>B)return; if(D>B)dd=INF; if(dc<x)x=dc,ans=C; if(dd<x)x=dd,ans=D; DFS(div*10); } int main() { freopen("bzoj4029.in","r",stdin); freopen("bzoj4029.out","w",stdout); for(int Case=gint();Case;Case--) { cin>>A>>B; x=INF; DFS(1); cout<<ans<<endl; } return 0; }