I could not find any article or blog on this idea, and felt having one could be helpful to many. There might be unwanted errors, so please feel free to to share any suggestions, corrections or concerns. For those who are unware of Binary Exponentation, this CP-Algorithms article or this video by Errichto can help get familiar with it.
Introduction
Now that we know the idea behind binary exponentiation; let us try expanding the idea. Say, we break the problem into two parts, $$$L$$$ and $$$R$$$, such that, once we have both the results, combining them gives us our final answer as $$$ans = L \odot R$$$, where $$$\odot$$$ is an associative binary operator. If we can find $$$R$$$ as a function of $$$L$$$ as in $$$R = f(L)$$$ fast enough, say $$$O(X)$$$, then our final answer can be found in $$$O(X\;log(n))$$$. The idea is simple enough, so we now have a look at some simple examples. For the sake of simplicity we assume problem size $$$n$$$ is of form $$$2^k$$$ , $$$ k \in \mathbb{Z} $$$ .
Example 1: Calculating $$$a^n$$$
We are starting from the simplest example. We know that $$$ans = \underbrace{a \cdot a \dots \cdot a \cdot a }_\text{ n times} $$$. If we take $$$R = f(L) = L$$$, where $$$L = a^{n/2}$$$; we can find $$$R$$$ in $$$O(1)$$$ and build our answer as -
$$$\newline$$$
We are able to compute $$$R'$$$ directly from $$$L'$$$, unlike other methods such as RMQ or Binary Lifting where we build our answer from already calculated $$$L$$$ and $$$R$$$ on the independent smaller ranges to later combine them to compute the the answer for the bigger range.
Time Complexity: Finding $$$R = f(L)$$$ is $$$O(1)$$$, so total time complexity is $$$O(log(n))$$$.
Example 2: Summation of GP Series
Considering a standard GP series, we find its summation as
$$$\newline$$$
If we have to find the summation under arbitrary MOD then it's not necessary that $$$(r-1)^{-1}$$$ exists, and so we alternatively express our sum as
which gives $$$R = f(L) = r^{\frac{n}{2}} \cdot L$$$.
To handle the case for odd $$$n$$$ during implementation, we can break series as
$$$\newline$$$
We can find $$$S$$$ as $$$(n - 1)$$$ is even, and final answer will be $$$= 1 + a \cdot S$$$.
Time Complexity: Computing $$$R = f(L)$$$ is $$$O(log(n))$$$, so our total time complexity becomes $$$O(log(n)^{2})$$$. But we can calculate powers of $$$r$$$ along with our series, so we restrict the final time complexity to $$$O(log(n))$$$.
We can use a similar idea for calculating GP series of matrices $$$ = I + A + A^2 + \dots + A^{n-1}$$$. Just like integers under modulo, we can't always guarantee $$$(A-I)^{-1}$$$ exists.
Contest Example: In AtCoder ABC 293 Task E the task is to just compute the sum of a GP series.
Example 3: Summation of AP-GP series
We start by considering the simple series $$$\displaystyle r + 2 \cdot r^2 + 3 \cdot r^3 \dots + (n-1)\cdot r^{n-1} = \sum\limits_{i=0}^{n-1} i\cdot r^i$$$. For this we express the summation as below
Now we solve for $$$R = f(L)$$$.
To handle the odd case during implementation, we express summation as:
$$$\newline$$$
We can get $$$X$$$ and $$$Y$$$ because they are of even length, and our final answer will be $$$= r \cdot X + r \cdot Y$$$ .
Time Complexity: To get $$$R$$$ we need to solve $$$L_{0}$$$ which takes $$$O(log(n))$$$ so total will be $$$O(log(n)^2)$$$, but we can calculate powers of $$$r$$$, $$$L_{0}$$$ and $$$L$$$ together which makes total time $$$O(log(n))$$$.
For any generic AP-GP series $$$\displaystyle = \sum\limits_{i=0}^{n-1} ( a + i \cdot b ) \cdot r^i$$$ $$$= a \cdot \underbrace{\sum\limits_{i=0}^{n-1} r^i}_\text{ L0 } + b \cdot \underbrace{\sum\limits_{i=0}^{n-1} i \cdot r^i}_\text{L} = a \cdot L_{0} + b \cdot L $$$, where both are being calculated with single function call.
AtCoder ABC 129 task F is a good practice example for the ideas in the blog.
Genralising the above series summations
Now, let us consider $$$\displaystyle S(n,m) = \sum\limits_{i=0}^{n-1} i^m \cdot r^i $$$. Then we would have GP $$$= S(n,0)$$$ and AP-GP $$$= S(n,1)$$$. Hence,
$$$\newline$$$
Here $$$L_{m,\frac{n}{2}} = S(\frac{n}{2},m)$$$. Then, $$$S(n,m) = L_{m,\frac{n}{2}} + R_{m,\frac{n}{2}}$$$, We need to compute $$$R_{m,\frac{n}{2}}$$$.
$$$\newline$$$
Now, swapping the summations -
Finally,
As $$$j \leq m$$$ if we calculate everything together, just like above and hence know the value of every $$$L_{j,\frac{n}{2}}$$$ without any extra time. We can validate our results for the above illustrated examples of AP and AP-GP series.
We can now evaluate our original summation as $$$\displaystyle S_{n,m} = S_{\frac{n}{2},m} + r^{\frac{n}{2}} \cdot \left( \sum\limits_{j=0}^{m} \binom{m}{j} \left( \frac{n}{2}\right) ^{m-j} \cdot S_{\frac{n}{2},j} \right)$$$.
Handling Odd Case
We have $$$\displaystyle S_{n,m} = \sum\limits_{i=0}^{n-1} i^m \cdot r^i = r \cdot \left( \sum\limits_{i=1}^{n-2} (i+1)^m \cdot r^i \right)$$$. However we can only write this if $$$m>0$$$ but for $$$m=0$$$ first term will not be $$$0$$$ but instead be $$$\displaystyle \lim_{x \to 0} x^x = 1$$$. So for $$$m=0$$$ we will add $$$1$$$ instead of $$$0$$$, keeping the remainder of the expression same. Thus
And swapping summations
Hence, we have the final result as:
We can also construct a clean visualization of the even and odd cases using matrix multiplication instead of summation. These are illustrated as below.
In the above cases, whenever we are computing values of $$$R_{j, n / 2} \hspace{0.25cm} \text{for} \hspace{0.25cm} j \in [0, m]$$$, they still indeed maintain our original form of $$$R = f(L)$$$ as we can express them as
Time Complexity: To find $$$R_{j,\frac{n}{2}}$$$ takes summation of $$$m$$$ terms and we do that for every $$$j \leq m $$$ which will take $$$O(m^2)$$$. So, total complexity is $$$O(m^2 \cdot log(n)).$$$
Nice blog!
btw S(n, m) can be calculated in subquadratic time. For more information, see the Forum section of this Library Checker problem.
Thanks for sharing this information. This is new to me and I will certainly have a look into it. Seems intriguing.
Example 2 even case can be much easier:
$$$a^0 + a^1 + \cdots + a^{n-1}$$$ = $$$a^0 + a^2 + a^4 + \cdots + a^1 + a^3 + a^5 + \cdots$$$ = $$$(a + 1) \times (a^0 + a^2 + \cdots + a^{n-2})$$$
Thanks for adding this easy to implement idea to the one shared in the blog! Will certainly come in handy.