B. Making Sequences is Fun 暴力比较水

B. Making Sequences is Fun
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

We'll define S(n) for positive integer n as follows: the number of the n's digits in the decimal base. For example, S(893) = 3,S(114514) = 6.

You want to make a consecutive integer sequence starting from number m (m, m + 1, ...). But you need to pay S(nk to add the number n to the sequence.

You can spend a cost up to w, and you want to make the sequence as long as possible. Write a program that tells sequence's maximum length.

Input

The first line contains three integers w (1 ≤ w ≤ 1016), m (1 ≤ m ≤ 1016), k (1 ≤ k ≤ 109).

Please, do not write the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cincout streams or the %I64dspecifier.

Output

The first line should contain a single integer — the answer to the problem.

Sample test(s)
input
9 1 1
output
9
input
77 7 7
output
7
input
114 5 14
output
6
input
1 1 2
output
0

 

const int maxn = 30000;
int digit(LL x)
{
    int ans = 0;
    while(x)
    {
        x/=10;
        ans++;
    }
    return ans;
}
LL  p10(int x)
{
    LL ans = 1;
    repf(i,1,x)
        ans *= 10;
    return ans;
}
int main() 
{
    //freopen("in.txt","r",stdin);
    LL w,m,k;
    while(cin>>w>>m>>k)
    {
        LL ans = 0;
        int d = digit(m);
        LL Max = 0;
        repf(i,1,d)
            Max = Max*10 + 9;
        LL L = Max - m + 1;
        if(L >= w/((LL)d*k))
        {
            ans = w/((LL)d*k);
        }else
        {
            ans += L;
            w -= L*(LL)d*k;
            int e = d+1;
            for(int i = d+1;i <= 17;++i)
            {
                LL P = p10(i-1)*9;
                if(w/((LL)i*k) >= P )
                {
                    w -= P*(LL)i*k;
                    ans += P;
                }else
                {
                    e = i;
                    break;
                }
            }
            if(w >= (LL)e*k)
            {
                ans += w / ((LL)e*k);
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

 

posted on 2013-12-15 16:41  keep trying  阅读(486)  评论(0编辑  收藏  举报

导航