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 | 3792 |
| 2 | VivaciousAubergine | 3647 |
| 3 | Kevin114514 | 3611 |
| 4 | jiangly | 3583 |
| 5 | strapple | 3515 |
| 6 | tourist | 3470 |
| 7 | Radewoosh | 3415 |
| 8 | Um_nik | 3376 |
| 9 | maroonrk | 3361 |
| 10 | XVIII | 3345 |
| # | User | Contrib. |
|---|---|---|
| 1 | Qingyu | 162 |
| 2 | adamant | 148 |
| 3 | Um_nik | 146 |
| 4 | Dominater069 | 143 |
| 5 | errorgorn | 141 |
| 6 | cry | 138 |
| 7 | Proof_by_QED | 136 |
| 8 | YuukiS | 135 |
| 9 | chromate00 | 134 |
| 10 | soullless | 133 |
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.