时间限制: 1 Sec 内存限制: 128 MB
[提交] [状态]
题目描述
Takahashi has come to an integer shop to buy an integer.
The shop sells the integers from 1 through 10^9. 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≤10^9
·1≤B≤10^9
·1≤X≤10^18
输入
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.
题目大意是求满足A×N+B×d(N) <=X
的最大的n值。显然f(n)=A×N+B×d(N)
为单增函数。因此考虑二分法求解,类似于高中学过的二分法求方程的根,算是比较简单的二分了。
注意求一个数的位数的方法:d=0;while(temp) { d++; temp=temp/10; }
注意二分的关键代码部分if(a*mid+b*d>x)r=mid-1; else if(a*mid+b*d<=x)l=mid+1;
不要写成if(a*mid+b*d>x)r=mid-1; else if(a*mid+b*d<x)l=mid+1; else break;
退出循环后r比l要小1,经过测验后发现最终应输出r,而且当找不到那个数时说明那个数应该是负的,l一直不动,r一直往左移动,直到等于0,恰好不用特判。
#include<stdio.h>
typedef long long int lli;
int main()
{
lli a;
lli b;
lli x;
lli mid;
lli temp;
lli l=1;
lli r=1e9;
lli d;
scanf("%lld %lld %lld",&a,&b,&x);
while(l<=r)
{
mid=(l+r)/2;
temp=mid;
d=0;
while(temp)
{
d++;
temp=temp/10;
}
if(a*mid+b*d>x)r=mid-1;
else if(a*mid+b*d<=x)l=mid+1;
}
printf("%lld",r);
return 0;
}
/**************************************************************
Language: C++
Result: 正确
Time:0 ms
Memory:1120 kb
****************************************************************/