AtCoder Beginner Contest 146 C - Buy an Integer

该问题描述了如何在给定预算内找到最划算的整数购买决策,涉及价格计算和算法优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 10910^9109. 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 ≤10910 ^ 9109

1 ≤ B ≤10910 ^ 9109

1 ≤ X ≤ 101810^{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;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值