codeforces 899D 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
Copy
7
output
3
input
Copy
14
output
9
input
Copy
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.


扫描二维码关注公众号,回复: 62641 查看本文章

题目大意就是说给你一个n,要你找到两个1-n的数加起来使得结果的末尾的9最长,并求出能达成该长度的方法数。

看样例就看出来,当n为5,50,500,5000时,最大长度len就会加1,所以就可以根据这个先把最大长度算出来,再枚举x9999中的x(0<=x<=8),计算每种x的方法数加起来就好了。

计算每种x情况下的方法数时,先算出目标数,比如x=1   len=1的情况 目标数就是 A=19 

1.    n>=(A-1)

两个数相加,让第一个数从1开始 第二个数从18开始  枚举到9 10 就结束了,所以方法数是A/2

2.     (A+1)/2<=n<(A-1)  

方法数就是n-(A+1)/2+1 

3.    n<(A+1)/2  

也就是说A=19时,n<9  那当然就是无法组成当前情况了


下面是代码

[cpp]  view plain  copy
  1. #include <bits/stdc++.h>  
  2. #include <cstring>  
  3. #define INF 0x3f3f3f3f  
  4. #define MOD 1e9+7  
  5. using namespace std;  
  6.   
  7. int main()  
  8. {  
  9.     long long n;  
  10.     cin>>n;  
  11.     if(n<5)  
  12.     {  
  13.         cout<<n*(n-1)/2<<endl;  
  14.         return 0;  
  15.     }  
  16.     int len=0;  
  17.     int num=n;  
  18.     while(1)  
  19.     {  
  20.         if(num<10)  
  21.             break;  
  22.         len++;  
  23.         num/=10;  
  24.     }  
  25.     if(num>=5)  
  26.         len++;  
  27.     long long k=0;  
  28.     for(int i=0;i<len;i++)  
  29.         k=k*10+9;  
  30.     long long ans=0;  
  31.     for(int i=0;i<=8;i++)  
  32.     {  
  33.         long long A=i*(k+1)+k;  
  34.         if(n<(A+1)/2)  
  35.             break;  
  36.         else if(n>(A-1))  
  37.             ans+=A/2;  
  38.         else  
  39.             ans+=n-(A+1)/2+1;  
  40.     }  
  41.     cout<<ans<<endl;  
  42.     return 0;  
  43.   
  44. }  

猜你喜欢

转载自blog.csdn.net/qq_39027601/article/details/80075795