CF899DShovel Sale

中文题意:从1-n挑两个数,加起来后缀9最多。输出有几组。

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

In the first example the maximum possible number of nines at the end is one. Polycarp cah choose the following pairs of shovels for that purpose:

  • 2 and 7
  • 3 and 6
  • 4 and 5

In the second example the maximum number of nines at the end of total cost of two shovels is one. The following pairs of shovels suit Polycarp:

  • 1 and 8
  • 2 and 7
  • 3 and 6
  • 4 and 5
  • 5 and 14
  • 6 and 13
  • 7 and 12
  • 8 and 11
  • 9 and 10

In the third example it is necessary to choose shovels 49 and 50, because the sum of their cost is 99, that means that the total number of nines is equal to two, which is maximum possible for n = 50.

先根据n判断最多有几个9,明显是以5 50 500来分的界限。

然后从0999,1999,2999,,,,,8999来分别考虑。对每一个算出l和r。

#include<bits/stdc++.h>
using namespace std;
#define  LL long long
const LL maxn = 200000+444;
LL a[13];
LL b[13];
void init()
{
    a[1]=9;
    b[1]=10;
    for(LL i=2;i<=12;i++)
        a[i]=a[i-1]*10+9,b[i]=b[i-1]*10;
}
LL n;

LL query(LL x,LL pos)
{
    LL cnt=x*b[pos]+a[pos];
    LL l=max(1LL,cnt-n);
    LL r=min(n,cnt-1);
    if(l<r) return (r-l+1)/2;
    return 0;


}

int main()
{
    init();
    scanf("%lld",&n);
    if(n<=4){
        printf("%lld\n",n*(n-1)/2);
    }else{
        LL now=5;
        LL pos=0;
        while(n>=now){
            pos++;
            now*=10;
        }
        LL ans=0;
        for(LL i=0;i<9;i++){
            ans+=query(i,pos);
        }
        printf("%lld\n",ans);
    }

    return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值