Making Sequences is Fun(簡單枚舉)

http://codeforces.com/problemset/problem/373/B

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 nto 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


    題意:S(n)表示一個數字的位數,w表示擁有的金錢,m表示起始數,k表示每一位數字的花費。問從m開始最多可以有多少個數。

     注意:理解題意後,把過程簡單化。注意 m的上限是10的16次方,m增加的上限超過10的16次方,所以初始化b數組的時候,記得弄大一些。

#include"iostream"
#include"cstring"
#include"cstdio"

#define maxn 50005
#define LL __int64
using namespace std;

LL w,m,k,cnt,ans;

LL b[20];
int main()
{
    b[0]=1,b[1]=10;
    for(int i=2;i<=20;i++)
    {
        b[i]=b[i-1]*10;
    }
    while(~scanf("%I64d%I64d%I64d",&w,&m,&k))
    {
        w/=k;
        ans=0;
       for(int i=0;i<20;i++)
            if(m<b[i])
           {
               cnt=i;
               break;
           }
       while(w)
       {
           if(w>=(b[cnt]-m)*cnt)
           {
               ans += b[cnt]-m;
               w -= (b[cnt]-m)*cnt;
               m = b[cnt];
           }
           else
           {
               ans += w/cnt;
               w=0;
           }
           cnt++;
       }
        printf("%I64d\n",ans);
    }
    return 0;
}


發佈了35 篇原創文章 · 獲贊 10 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章