Codeforces Round #452 (Div. 2) D
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
這個題太容易丟失精度了 坑死我了 wa 一天
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
long long s=0;
cin>>n;
if(n<5){
cout<<n*(n-1)/2<<endl;
return 0;
}
long long m=n*2-1;
int i=0;
for(int j=0;j<10;j++){
long long k=pow(10,j);
if(k-1<=m){
i=j;
}
}
m=1;
for(int j=1;j<=i;j++){
m*=10;
}
long long z=m-1;
//cout<<m<<" "<<z<<endl;
for(int j=0;j<10;j++){
long long k=1LL*j*m+z;
if(k<=n){
s+=(k-1)/2;
}
else{
s+=max(1LL*0,(n-(k-n)+1)/2);
}
}
cout<<s<<endl;
return 0;
}
/*
49839
4999 9999
14998 19999
29997 29999
49996 39999
74836 49999
94676 59999
109516 69999
119356 79999
124196 89999
*/
相關文章
- Codeforces Round #452 (Div. 2) C
- Codeforces Round 941 (Div. 2) D
- Codeforces Round 960 (Div. 2)(A - D)
- Codeforces Round #325 (Div. 2) D bfs
- Codeforces Round #250 (Div. 2) A-D
- Codeforces Round #251 (Div. 2) A/B/D
- Codeforces Round #283 (Div. 2) D,E
- Codeforces Round #256 (Div. 2)A-D
- Codeforces Round #358 (Div. 2) D dp
- Codeforces Round #359 (Div. 2) D DFS
- Educational Codeforces Round 34 (Rated for Div. 2) D
- Codeforces Round #290 (Div. 2) A,B,C,D
- Codeforces Round #263 (Div. 2) A-D
- Codeforces Round 720 (Div. 2) A-D
- Codeforces Round #673 (Div. 2)(A-D)題解
- Codeforces Round #401 (Div. 2)(C,D,E)
- Codeforces Round #321 (Div. 2) D 狀壓dp
- Codeforces Round #360 (Div. 2) D 數學題
- Codeforces Round #288 (Div. 2) A,B,C,D,E
- Codeforces Round #287 (Div. 2)A,B,C,D,E
- Codeforces Round #253 (Div. 2) D. Andrey and Problem
- Codeforces Round #280 (Div. 2 A,B,C,D,E)
- Educational Codeforces Round 170 (Rated for Div. 2) A-D
- 【數論】Codeforces Round #404 (Div. 2)(D)Anton and School - 2
- Educational Codeforces Round 142 (Rated for Div. 2) A-D
- Codeforces Round #336 (Div. 2) D 區間dp
- Codeforces Round 960 (Div. 2) 補題記錄(A~D)
- CodeForces Round #951(Div. 2) 補題記錄(A~D)
- Codeforces Round 986 (Div. 2)題解記錄(A~D)
- Codeforces Round 987 (Div. 2)題解記錄(A~D)
- Codeforces Round #639 (Div. 2)
- Codeforces Round #541 (Div. 2)
- Codeforces Round #682 (Div. 2)
- Codeforces Round #678 (Div. 2)
- Codeforces Round #747 (Div. 2)
- Codeforces Round #673 (Div. 2)
- Codeforces Round #672 (Div. 2)
- Codeforces Round #448 (Div. 2) A