Codeforces Round #452 (Div. 2) D. Shovel Sale

D. Shovel Sale
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There are n shovels in Polycarp's shop. The i-th shovel costs i burles, that is, the first shovel costs 1 burle, the second shovel costs 2burles, the third shovel costs 3 burles, and so on. Polycarps wants to sell shovels in pairs.

Visitors are more likely to buy a pair of shovels if their total cost ends with several 9s. Because of this, Polycarp wants to choose a pair of shovels to sell in such a way that the sum of their costs ends with maximum possible number of nines. For example, if he chooses shovels with costs 12345 and 37454, their total cost is 49799, it ends with two nines.

You are to compute the number of pairs of shovels such that their total cost ends with maximum possible number of nines. Two pairs are considered different if there is a shovel presented in one pair, but not in the other.

Input

The first line contains a single integer n (2 ≤ n ≤ 109) — the number of shovels in Polycarp's shop.

Output

Print the number of pairs of shovels such that their total cost ends with maximum possible number of nines.

Note that it is possible that the largest number of 9s at the end is 0, then you should count all such ways.

It is guaranteed that for every n ≤ 109 the answer doesn't exceed 2·109.

Examples
input
7
output
3
input
14
output
9
input
50
output
1
Note

题意:对给定数n,从1~n任取两个数求和后,结果的末尾连续9的个数最大,问这样的数有几对。

思路:既然题意是和为求连续最多个数的9的数对,那么先求最多个9有多少个,由于不能自己加自己,对于给定

数n,总和最大是n+(n-1)就是最大加次大。那么除非最大值最高位是9,否则最大连续9的个数一定是最大值位数

减去1。求和得到的数最高位可以是0~9。

如果可以存在这样的和x,那么一对数中较大的数最大是x-1,因为另一个数最小不能是0。然后x-2与2是第二对

类推下去,那一对数中较大数的最小值是多少呢?当 x-y,y   是一对数时,较大值最小满足 x-y-y = 1因为这个x一定是9

结尾的(特殊情况是0个9结尾,可以直接求有多少对,就是答案),那就是奇数,两个数不会相等只能差1。那么

y = (x-1)/2,较大值就是 (x-1)/2 + 1。 则 x-1 -[ (x-1)/2 + 1 ] + 1。当前最高位情况下的答案数。

较大值也不能大于n,所以需要跟n比较去较小的。

n = int(raw_input())
x = n + n - 1 
s = 9 
while s <= x:
    s = s * 10 + 9 
s /= 10
ans = 0 
for i in xrange(0, 9): 
    t = (s + 1) * i + s 
    r = min(n, t - 1)
    l = t / 2 + 1 
    ans += max(r - l + 1, 0)
    
print ans 
代码里面没有单独处理没有9的情况,因为情况跟有9的时候一样。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值