long long calc(long long a) {
if (a < 10) return a;
long long r = a / 10;
r += 8;
long long f = a;
while (f >= 10) f /= 10;
if (f <= a % 10) r++;
return r;
}
# | User | Rating |
---|---|---|
1 | ecnerwala | 3649 |
2 | Benq | 3581 |
3 | orzdevinwang | 3570 |
4 | Geothermal | 3569 |
4 | cnnfls_csy | 3569 |
6 | tourist | 3565 |
7 | maroonrk | 3531 |
8 | Radewoosh | 3521 |
9 | Um_nik | 3482 |
10 | jiangly | 3468 |
# | User | Contrib. |
---|---|---|
1 | maomao90 | 174 |
2 | awoo | 164 |
3 | adamant | 162 |
4 | TheScrasse | 159 |
5 | nor | 158 |
6 | maroonrk | 156 |
7 | -is-this-fft- | 151 |
8 | SecondThread | 147 |
9 | orz | 146 |
10 | pajenegod | 145 |
long long calc(long long a) {
if (a < 10) return a;
long long r = a / 10;
r += 8;
long long f = a;
while (f >= 10) f /= 10;
if (f <= a % 10) r++;
return r;
}
Name |
---|
Function
calc(a)
computes number of all good numbers which are less of equal to a.if (a < 10) return a;
— in case of one-digit input.It's simple to prove that each tenth number is good (first and last digits are equal):
Don't forget about one-digit numbers:
Let's compute the first digit f of the number a
and check whether (a div 10 * 10 + f) <= a:
"It’s simple to prove that each tenth number is good (first and last digits are equal): long long r = a / 10;" not able to understand the above lines and one digit numbers should be 1 to 9(why 8) please explain??
Ok, assume that a % 10 == 0. (Last string checks another case)
For t > 0 every interval [ t * 10, (t + 1) * 10 ) contains exactly one good number. So the interval [10, a) contains
(a - 10) / 10
good numbers, ora / 10 - 1
. Add nine one-digit numbers and geta / 10 - 1 + 9 = a / 10 + 8
.