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.
The first line contains a single integer n (2 ≤ n ≤ 109) — the number of shovels in Polycarp's shop.
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.
7
3
14
9
50
1
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最多。(这两个数不能相同)
题解:我们可以暴力两个数的和的最高位是多少,比如说不管怎样组合末尾最多只有3个9,呢可能的和一定是:999、1999......89999
所以我们直接暴力最高位就好了,对于n<=4,因为无论如何都不能出现1个9,呢答案一定就是n*(n-1)/2了。
#include<stdio.h>
#include<algorithm>
using namespace std;
#define ll long long
ll n,ans;
ll work(ll x)
{
ll res=0;
if(x<=n) return max(res,(x-1)/2);
else return max(res,(n-(x-n)+1)/2);
}
int main(void)
{
scanf("%lld",&n);
if(n<=4)
{
printf("%lld\n",n*(n-1)/2);
return 0;
}
ll m=999999999;
while(2*n-1<m) m/=10;
int t=1;
while(t<m) t*=10;
for(ll i=0;i<=9;i++)
{
if(i*t+m<=2*n-1)
ans+=work(i*t+m);
else
break;
}
printf("%lld\n",ans);
return 0;
}