Official
B - Sum of Digits Sequence Editorial by en_translator
Once you implement the function \(f\) that evaluates \(f(x)\) for any given \(x\), all that left is to iteratively compute \(A_0, A_1, \ldots, A_N\) to find the answer.
The approaches to implement \(f\) include: repeatedly finding the remainder when \(x\) is divided by \(10\) and then dividing it by \(10\), or converting \(x\) it into a string.
Sample code
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
auto f = [&](int x) -> int {
int res = 0;
while (x) {
res += x % 10;
x /= 10;
}
return res;
};
vector<int> a(n + 1);
a[0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < i; j++) a[i] += f(a[j]);
}
cout << a[n] << '\n';
}
posted:
last update: