Official

C - Calculator Editorial by evima


Consider the following alternation: Operation \(4 \rightarrow 3 \rightarrow 4 \rightarrow 3 \rightarrow \cdots\). Let us say we did a total of \(S\) operations here. For convenience, we assume \(S\) to be even.

For each \(i\) (\(0 \leq i \leq S\)), consider inserting Operation \(1\) or \(2\) just after the \(i\)-th operation above. We can see the following:

  • If \(i\) is even: doing Operation \(1\) once increases the final value of \(x\) by \(fib(S-i)\).
  • If \(i\) is odd: doing Operation \(2\) once increases the final value of \(x\) by \(fib(S-i)\).

Here, \(fib(n)\) denotes the Fibonacci number, where \(fib(0)=1\), \(fib(1)=1\), and \(fib(n)=fib(n-1)+fib(n-2) \) for \(2 \leq n\).

Let us consider doing Operation 1 and 2 at most once in total for each \(i\). In this case, we should choose the largest \(S\) such that \(fib(S) \leq 10^{18}\), since it does not help to choose a greater \(S\). A calculation shows that we should choose \(S=86\).

What remains is to solve the problem of representing \(N\) as the sum of \(fib(S-i)\). We can solve it greedily: in ascending order of \(i\), we should use the value whenever possible. Here, we will never take \(i\) and \(i+1\) at the same time, since we would take \(i-1\) instead in that case. Thus, we will do Operation \(1\) and \(2\) at most \(S/2+1=44\) times in total (in reality, at most \(43\) times).

Therefore, we will have at most \(86+44=130\) operations in total, which solves the problem.

posted:
last update: