Problem solution · C++

ABC414 C — Palindromic in Both Bases

ABC414 C — Palindromic in Both Bases: a C++ solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to michimani AtCoder Solutions.

Technique
Direct simulation
Source
michimani AtCoder Solutions
Length
63 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For ABC414 C — Palindromic in Both Bases, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 63 lines of C++ from the credited upstream file abc414_c.cpp.
  • The implementation visibly relies on sequence storage.
  • 8 loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from michimani AtCoder Solutions by michimani and is used under the MIT licence.

Full codeABC414 C — Palindromic in Both Bases · C++C++
Use this to learn the idea, then write your own version.
#include <iostream>#include <set>#include <vector> using namespace std;using ull = unsigned long long; bool isPalindrome(ull num, int base) {    vector<int> digits;    ull temp = num;    while (temp > 0) {        digits.push_back(temp % base);        temp /= base;    }     int len = digits.size();    for (int i = 0; i < len / 2; i++)        if (digits[i] != digits[len - 1 - i]) return false;     return true;} void generatePalindromes(vector<ull>& palindromes, ull maxN) {    for (int len = 1; len <= 12; len++) {        if (len == 1) {            for (ull i = 1; i <= 9; i++)                if (i <= maxN) palindromes.push_back(i);        } else {            int halfLen = (len + 1) / 2;            ull start = 1;            for (int i = 1; i < halfLen; i++) start *= 10;             ull end = start * 10;             for (ull half = start; half < end; half++) {                string halfStr = to_string(half);                string palindrome = halfStr;                 int startIdx = (len % 2 == 0) ? halfLen - 1 : halfLen - 2;                for (int i = startIdx; i >= 0; i--) palindrome += halfStr[i];                 ull num = stoull(palindrome);                if (num <= maxN) palindromes.push_back(num);            }        }    }} int main() {    int a;    ull n;    cin >> a >> n;     vector<ull> palindromes;    generatePalindromes(palindromes, n);     ull sum = 0;    for (ull num : palindromes)        if (isPalindrome(num, a)) sum += num;     cout << sum << endl;    return 0;}

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗