Bootstrap

AtCoder Beginner Contest 146 C - Buy an Integer

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 300 points

Problem Statement

Takahashi has come to an integer shop to buy an integer.

The shop sells the integers from 1 through 1 0 9 10^9 109. The integer N is sold for A×N+B×d(N) yen (the currency of Japan), where d(N) is the number of digits in the decimal notation of N.

Find the largest integer that Takahashi can buy when he has X yen.
If no integer can be bought, print 0.

Constraints

All values in input are integers.

1 ≤ A ≤ 1 0 9 10 ^ 9 109

1 ≤ B ≤ 1 0 9 10 ^ 9 109

1 ≤ X ≤ 1 0 18 10^{18} 1018

Input

Input is given from Standard Input in the following format: A B X

Output

Print the greatest integer that Takahashi can buy. If no integer can be bought, print 0.

Sample Input 1

10 7 100

Sample Output 1

9

The integer 9 is sold for 10×9+7×1=97 yen, and this is the greatest integer that can be bought. Some of the other integers are sold for the following prices:

10: 10 × 10 + 7 × 2 = 114 yen
100:10 × 100 + 7 × 3 = 1021 yen
12345:10 × 12345 + 7 × 5 = 123485 yen

Sample Input 2

2 1 100000000000

Sample Output 2

1000000000

He can buy the largest integer that is sold. Note that input may not fit into a 32-bit integer type.

Sample Input 3

1000000000 1000000000 100

Sample Output 3

0

Sample Input 4

1234 56789 314159265

Sample Output 4

254309

Solution

#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
const LL N = 1e9;

LL Min(int cnt) {
    string s1 = "";
    
    s1 += '1';
    for(int i = 1; i <= cnt - 1; i ++) s1 += '0';
    
    return (LL) stoi(s1);
}

LL Max(int cnt) {
    string s2 = "";
    for(int i = 1; i <= cnt; i ++) s2 += '9';
    return (LL) stoi(s2);
}

int main() {
    LL a, b, x;
    cin >> a >> b >> x;
    
    if(a * 1 + b * 1 > x) {
        cout << 0 << '\n';
    } else if(a * N + b * 10 < x) {
        cout << N << '\n';
    } else {
        LL ans = 0;
        
        for(int i = 1; i <= 9; i ++) {
            LL x1 = Min(i), x2 = Max(i);
            
            if(x2 * a + i * b <= x) ans = max(ans, x2);
            else if(x1 * a + i * b <= x) {
                LL res = (x - i * b) / a;
                ans = max(ans, res);
            } else break;
        }
        
        cout << ans << '\n';
    }
    
    return 0;
}
;