---
id: 256
lang: en
translation_of: index.md
source_sha256: 2119ddf70bd31db37634268d7cc1aeebe24143b681cc629bff8eb23c3f2f60e2
title: "P2110: The Tale of Boss Huan Shouting Up at the Building — Solution"
description: "Counting numbers in a range whose first and last digits match, with a direct formula instead of digit DP."
tags: ["OI","Godlike"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "Problem name 欢总喊楼记 rendered as “The Tale of Boss Huan Shouting Up at the Building”: 欢总 is a nickname (Huan + 总, “boss”), and 喊楼 is shouting up at a (dorm) building."
  - "最低尾 (literally “lowest tail”) rendered as “the lowest digit”."
---

## A <span class="slang" data-zh="神仙" data-gloss="shénxiān, “immortal”. Someone, or a problem or idea, so brilliant it seems superhuman.">Godlike</span> Approach

After reading the solutions I felt really <span class="slang" data-zh="心塞" data-gloss="xīnsāi, “heart blocked”. Internet slang for feeling frustrated, gutted, and down.">gutted</span>: it doesn't even need digit DP! Let's first consider a partial solution. When $num<10$, the answer is just $num$. If $num \geq 10$, the answer is $9+\frac{x}{10}$. Let's see: consider the answer for the number $1921$. We can consider that the $192-$ part contains $\lfloor \frac{1920}{10} \rfloor$ pairs of numbers whose first and last digits are the same. Then we add the answer for when there is only one digit.

<!--more-->

We also have to consider this: if the highest digit of $num$ is greater than the lowest digit, the answer has to be decreased by one. For example, the number $62$ needs extra consideration, because on the surface the second digit has $6$ pairs of numbers matching the first digit, but that's not actually so: since $66>62$, this possibility has to be excluded.

Here's the code:

```cpp
// P2110.cpp
#include <iostream>
#include <cstdio>
#define ll long long
using namespace std;
ll L, R;
ll dp[20][20][20];

ll query(ll num)
{
    if (num <= 9)
        return num;
    ll sum = 9 + num / 10;
    ll inner = num % 10;
    ll highest = num;
    while (highest >= 10)
        highest /= 10;
    if (highest > inner)
        sum--;
    return sum;
}

int main()
{
    scanf("%lld%lld", &L, &R);
    printf("%lld", query(R) - query(L - 1));
    return 0;
}
```
