问题 J: Buy an Integer-------------------------二分

28 篇文章 0 订阅

题目描述
Takahashi has come to an integer shop to buy an integer.
The shop sells the integers from 1 through 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≤109
·1≤B≤109
·1≤X≤1018
输入

Input is given from Standard Input in the following format:

A B X
输出
Print the greatest integer that Takahashi can buy. If no integer can be bought, print 0.
样例输入 Copy
【样例1】

10 7 100
【样例2】
2 1 100000000000
【样例3】
1000000000 1000000000 100
【样例4】
1234 56789 314159265
样例输出 Copy
【样例1】

9
【样例2】
1000000000
【样例3】
0
【样例4】
254309
提示
样例1解释

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
样例2解释
He can buy the largest integer that is sold. Note that input may not fit into a 32-bit integer type.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a,b,x;
bool check(ll m)
{
	ll res=m;
	ll len=0;
	while(res)
	{
		len++;
		res/=10;
	}
	if(a*m+b*len<=x) return 1;
	else return 0;
}
int main() 
{
	cin>>a>>b>>x;
	ll l=0,r=1e9;
	while(l<r)
	{
		ll mid=l+r+1>>1;
		if(check(mid)) l=mid;
		else r=mid-1;
	 } 
	 if(check(l)) cout<<l<<endl;
	 else cout<<0<<endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值