Editorial for Bedao Regular Contest 15 - SYMMETRY


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Nhận thấy một số đối xứng luôn có nửa đầu và nửa cuối là một số giống nhau (chỉ đảo ngược thứ tự). Ví dụ:

  • ~1221~ có nửa đầu là ~12~ và nửa cuối là số đảo ngược của ~12~
  • ~45654~ có nửa đầu là ~45~ và nửa cuối là số đảo ngược của ~45~

Với giới hạn ~L \le R \le 10^{9}~ thì nửa đầu và nửa sau của các số đối xứng cần đếm là một số ~x \lt 10^4~. Ta có thể duyệt qua các số ~x \lt 10^4~ và tạo ra các số đối xứng tương ứng.

Code mẫu

#include<bits/stdc++.h>
using namespace std;

long long L, R;

void solve()
{
    int MAXV = 1e6;

    long long res = 0;
    for (int i = 1; i <= MAXV; i++)
    {
        int x = i;
        long long rev = 0;
        long long p = 1;
        while (x)
        {
            rev = rev * 10 + (x % 10);
            x /= 10;
            p *= 10;
        }

        if (i < 10 && i >= L && i <= R) res += i;

        if (p * i + rev >= L && p * i + rev <= R)
            res += p * i + rev;

        for (int c = 0; c < 10; c++)
        {
            long long val = i * 10 + c;
            val = val * p + rev;
            if (val >= L && val <= R) res += val;
        }
    }
    cout << res;
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0);
    cin >> L >> R;
    solve();
}

Comments

Please read the guidelines before commenting.


There are no comments at the moment.