Official

B - Fibonacci Reversed Editorial by en_translator


Basically, compute \(a_3,a_4,\dots,a_{10}\) just as specified in the problem statement. The implementation requires a mutual conversion between integers and strings, and reversion of strings. While it is not difficult to implement by yourself (for example, using a for statement), but many programming languages provides them as a part of standard features, so it is convenience to use them. For C++ and Python, please refer to the sample code below.

Note that the answer may not fit into a 32-bit integer type (like int type in C++).

Sample code (C++ and Python):

#include <bits/stdc++.h>

using namespace std;

int main() {
    vector<long long> a(10);
    cin >> a[0] >> a[1];
    for (int i = 2; i < 10; i++) {
        string s = to_string(a[i - 2] + a[i - 1]);
        reverse(s.begin(), s.end());
        a[i] = stoll(s);
    }
    cout << a[9] << endl;
}
a = [0] * 10
a[0], a[1] = map(int, input().split())

for i in range(2, 10):
    s = str(a[i - 2] + a[i - 1])
    a[i] = int(s[::-1])
  
print(a[9])

posted:
last update: