cf #452(div2) D

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 2 burles, 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

Copy

7

Output

Copy

3

Input

Copy

14

Output

Copy

9

Input

Copy

50

Output

Copy

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.

题目链接:http://codeforces.com/contest/899/problem/D

题意:求 1<= a , b <= n , 记 n = a +b求末尾有最多连续的9的n。求1-n中有多少组数据使得和玮玮n,不同的数据当且仅当有一个数不同。
写几个数之后可以找规律,当n的最高位>=5时,最大末尾连续9是n的位数,否者是n的位数-1.
因此只需要枚举第一位数字来判断。

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

typedef long long ll;
ll n, t;
ll a[15], b[15];

int main()
{
    cin >> n;
    if(n < 5)
    {
        cout << (n-1)*n/2 << endl;
        return 0;
    }
    a[1] = b[1] = 1;
    a[2] = 10, b[2] = 9;
    for(int i = 3; i <= 12; i++)
    {
        a[i] = a[i-1]*10;
        b[i] = b[i-1]*10+9;
    }
    t = n;
    int cnt = 0, c;
    while(t)
    {
        c = t%10;
        t /= 10;
        cnt++; // 算9的个数 
    }
    if(c >= 5)
        cnt++; 
    ll ans = 0;
    for(int i = 0; i <= 8; i++)
    {
        ll sum = i*a[cnt]+b[cnt];
        if((sum+1)<=2*n && n<=sum-1)
            ans += n-(sum+1)/2+1;   
        else if(n >= sum)
            ans += (sum-1)/2;  // 包含所有的可能组合 例如n=14 ,sum=9.则所有的组合是4 
    }
    cout << ans << endl;
} 

猜你喜欢

转载自blog.csdn.net/qq_38295645/article/details/81742250
今日推荐