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 | Benq | 3676 |
| 2 | Kevin114514 | 3659 |
| 3 | jiangly | 3640 |
| 4 | turmax | 3450 |
| 5 | tourist | 3382 |
| 6 | strapple | 3360 |
| 7 | Um_nik | 3354 |
| 8 | squareOf105 | 3348 |
| 9 | heuristica | 3322 |
| 10 | maroonrk | 3312 |
| # | User | Contrib. |
|---|---|---|
| 1 | Qingyu | 157 |
| 2 | nik_exists | 151 |
| 3 | maspy | 149 |
| 4 | AmShZ | 144 |
| 5 | Um_nik | 143 |
| 6 | Errichto | 139 |
| 7 | adamant | 137 |
| 8 | BledDest | 133 |
| 9 | maroonrk | 132 |
| 10 | Proof_by_QED | 131 |
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) / 10good numbers, ora / 10 - 1. Add nine one-digit numbers and geta / 10 - 1 + 9 = a / 10 + 8.