http://codeforces.com/contest/373/problem/B
用二分枚举长度就可以。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define LL __int64 5 using namespace std; 6 7 LL w,m,k; 8 9 bool ok(LL c) 10 { 11 LL ans=0; 12 LL x2=m+c-1; 13 int t1=0; 14 LL b=x2; 15 while(b) 16 { 17 b/=10; 18 t1++; 19 } 20 int t2=0; 21 LL b1=m; 22 while(b1) 23 { 24 b1/=10; 25 t2++; 26 } 27 if(t1==t2) 28 { 29 ans+=c*t1*k; 30 if(ans<=w&&ans>=0) return true; 31 else return false; 32 } 33 LL x1=1; 34 int a1=t1-1; 35 while(a1--) 36 { 37 x1*=10; 38 } 39 ans+=(x2-x1+1)*t1*k; 40 LL x3=1; 41 int a2=t2; 42 while(a2--) 43 { 44 x3*=10; 45 } 46 ans+=(x3-m)*t2*k; 47 for(LL i=x3; i<x1; i*=10) 48 { 49 t2++; 50 ans+=(i*10-i)*t2*k; 51 } 52 if(ans<=w&&ans>=0) return true; 53 else return false; 54 55 } 56 57 int main() 58 { 59 while(scanf("%I64d%I64d%I64d",&w,&m,&k)!=EOF) 60 { 61 LL l=1,r=w; 62 LL ans=0; 63 while(l<=r) 64 { 65 LL mid=(l+r)/2; 66 if(ok(mid)) 67 { 68 ans=mid; 69 l=mid+1; 70 } 71 else 72 r=mid-1; 73 } 74 printf("%I64d\n",ans); 75 } 76 return 0; 77 }