Official

E - Arithmetic Number Editorial by en_translator


The most important fact in this problem is that “there are not so many arithmetic numbers less than \(10^{18}\).” Let’s first estimate this.
Consider the candidates of arithmetic sequences behind that yield arithmetic numbers (for example, \((2,4,6,8) \rightarrow 2468\)). The arithmetic sequence should necessarily satisfy the following condition.

  • The initial term is between \(1\) and \(9\) (inclusive).
  • The common difference is between \(-9\) and \(8\) (inclusive).
  • The length of sequence is between \(1\) and \(18\) (inclusive).

(Even if the sequence of length \(1\) is counted more than once,) The number of sequences that satisfy these conditions is at most \(9 \times 18 \times 18 = 2916\). However, even if these conditions are met, a sequence like \((7,1,-5)\) does not turn into an arithmetic number. The following additional condition is required.

  • The second and later terms are all between \(0\) and \(9\) (inclusive).

It is sufficient that the four conditions listed above are satisfied. All that left is to enumerate all the arithmetic integers based on this, and output the smallest one that is no less than \(X\). Note that for \(X=10^{17}\) the answer is \(111111111111111111\), so we do not have to consider the arithmetic numbers more than or equal to \(10^{18}\).

This solution operates in a constant time, as the enumeration of arithmetic numbers finishes in a constant time.

Sample code (C++):

#include<bits/stdc++.h>

using namespace std;

set<long long> gen_tosasu(){
  set<long long> res;
  for(int fir=1;fir<=9;fir++){
    for(int d=-9;d<=8;d++){
      string s;
      int dg=fir;
      for(int i=0;i<18;i++){
        s.push_back(dg+'0');
        res.insert(stoll(s));
        dg+=d;
        if(!(0<=dg && dg<=9)){break;}
      }
    }
  }
  return res;
}

int main(){
  long long x;
  cin >> x;
  set<long long> st=gen_tosasu();
  cout << (*st.lower_bound(x)) << '\n';
  return 0;
}

posted:
last update: