D. Shovel Sale
time limit per test 1 second
memory limit per test 256 megabytes
There are n shovels in Polycarp's shop. The i-th shovel costs i burles, thatis, the first shovel costs 1 burle, thesecond shovel costs 2 burles, the thirdshovel costs 3 burles, and so on.Polycarps wants to sell shovels in pairs.
Visitors aremore 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 insuch a way that the sum of their costs ends with maximum possible number ofnines. For example, if he chooses shovels with costs 12345 and 37454, their totalcost is 49799, it ends with two nines.
You are tocompute the number of pairs of shovels such that their total cost ends withmaximum possible number of nines. Two pairs are considered different if thereis a shovel presented in one pair, but not in the other.
Input
The first linecontains a single integer n (2 ≤ n ≤ 109) — the number of shovels in Polycarp's shop.
Output
Print the numberof pairs of shovels such that their total cost ends with maximum possiblenumber of nines.
Note that it ispossible that the largest number of 9s at the end is 0, then you should countall 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 firstexample the maximum possible number of nines at the end is one. Polycarp cahchoose the following pairs of shovels for that purpose:
· 2 and 7;
· 3 and 6;
· 4 and 5.
In the secondexample the maximum number of nines at the end of total cost of two shovels isone. 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 tochoose 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 ismaximum possible for n = 50.
【题意】
现在给你1~n共n个数,现在你首先需要得到去两个数相加它们结尾9个数最多为多少(x),然后再找出去两个数使得它们相加和末位有x个0的方案数。
【思路】
我们先找出对于一个给定的n,其末位最多有几个9.
然后举个例子,如x=2,那么我们只要去枚举结尾有两个2的数字作为和,如99,199,299,399,499,599....899,然后只要去判断下哪些组合满足条件即可,具体见代码。
【PS】注意n<=4的特殊情况,应该把所有方案数输出。
#include <cstdio>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)
typedef long long ll;
const int maxn = 1000005;
const ll mod = 1e9+7;
const ll INF = 0x3f3f3f3f;
const double eps = 1e-9;
ll n;
ll a[maxn];
void init()
{
a[1]=9;
for(int i=2;i<=12;i++) a[i]=a[i-1]*10+9;
}
ll power(ll x, ll n)
{
ll ans=1;
while(n)
{
if(n&1) ans*=x;
x*=x;
n>>=1;
}
return ans;
}
ll solve(int x,int pos)
{
ll cnt=x*power(10,pos)+a[pos]; //枚举结尾有pos个9的数
ll l=cnt-n,r=min(n,cnt-1);
if(l<=0) l=1;
if(l<=r) return(r-l+1)/2;
return 0;
}
int main()
{
init();
while(~scanf("%I64d",&n))
{
if(n<=4)
{
printf("%I64d\n",(n-1)*n/2);
continue;
}
else
{
int pos;
for(int i=1;i<12;i++)
{
if(n*2<a[i]) //举个例子:n为50,最大为49+50,pos=2
{
pos=i-1;
break;
}
}
ll ans=0;
for(int i=0;i<=8;i++)
{
ans+=solve(i,pos);
}
printf("%I64d\n",ans);
}
}
return 0;
}