Following the solution to my previous post in: Counting unique element output from a product of combination
where the number of unique string-variants generated from a given string is:
$$ 1+\sum_{k=1}^r 3^k\binom{n}{k} $$
How to solve the summation so that I could use this formula for large $r$?
My attempt: I try to solve it using a general formula: $$ S\{n\} = \frac{a_1(1-r^n)}{1-r} $$ for $a = 3$ and the common factor $r = 3$. But it turns out that is wrong since the formula above did not has anything to do with the binomial coefficient. Anyone can help?
This sum does not have an elementary representation. Let $f(r,n)$ be your sum. By a CAS \begin{align*} f(r,n) &= 1 + \sum_{k=1}^r 3^k \binom{n}{k} \\ &= 4^n - 3^{r+1}\binom{n}{r+1}{}_2F_1(1,r-n+1;r+2;-3) \end{align*}
where ${}_2F_1$ is the Gaussian hypergeometric function. Even assuming $r$ and $n$ integers with $0 < r < n$, there are no simplifications to elementary functions.
For $1 \leq r < n$, use the sum. This is easy as long as $n$ is not large. You give no hints about the size of $n$ in your question. Your prior post claims you are only interested in $1 \leq r \leq 4$, so this sum is nearly immediate.
For $r \geq n$, $f(r,n) = 4^n$. This follows from your sum since $\binom{n}{n+1} = \binom{n}{n+2} = \cdots = 0$, so the sum cuts itself off after $r = n$, and a standard identity (first one at that link, taking $x = 3$ and realizing the $k = 0$ term is the "$1+$" in $f$). In the context of your previous post (not having read it in detail) -- once you've decided to replace every character in your string, then you have any $n$-length string you want from your 4 character alphabet, giving $4^n$ possibilities.
Digging through OP's referenced Question, he most likely intends $1 \leq r \leq 4$ and $r \leq n$. These constraints appear nowhere in the current Question. With those constraints ... \begin{align*} f(1,n) &= 1 + 3n \text{,} \\ f(2,n) &= 1 + 3n + \frac{9}{2}n(n-1) \text{,} \\ f(3,n) &= \frac{1}{2}(9n^2 -3n+2) + \frac{9}{2}n(n-1)(n-2) \text{, and} \\ f(4,n) &= \frac{1}{2}(9n^3 - 18 n^2 + 15 n + 2) + \frac{27}{8}n(n-1)(n-2)(n-3) \text{.} \end{align*}
For example, $f(4,10\,000) = 33\,734\,252\,812\,372\,501$. I note this because representing such quantities in a programming language as precise integers (instead of imprecise floating point approximations) can require some effort. Letting $r$ and $n$ be "large" necessitates this effort. If you intend to generate these numbers in a programming language, it would help to know which language you are targetting.
Just testing for timing, I get the complete set of results for $n = 10\,000$ in about 180 ms using this code:
Don't bother trying to print the last few terms. $4^{10\,000}$ is too large to usefully look at.
Note that
partialSums[k-1]is $f(k,n)$ for $k \leq n$.\begin{align*} f(1,n) &= 1 + \sum_{k=1}^1 3^k \binom{n}{k} \\ &= 1 + 3^1 \binom{n}{1} \\ &= 1 + 3 n \text{, } \\ f(2,n) &= 1 + \sum_{k=1}^2 3^k \binom{n}{k} \\ &= 1 + 3^1 \binom{n}{1} + 3^2 \binom{n}{2} \\ &= 1 + 3 n + 9\frac{n(n-1)}{2} \text{, } \\ f(3,n) &= 1 + \sum_{k=1}^3 3^k \binom{n}{k} \\ &= 1 + 3^1 \binom{n}{1} + 3^2 \binom{n}{2} + 3^3 \binom{n}{3} \\ &= 1 + 3 n + 9\frac{n(n-1)}{2} + 27\frac{n(n-1)(n-2)}{6} \\ &= \frac{1}{2}(2 + 6n + 9n(n-1)) + \frac{9}{2}n(n-1)(n-2) \\ &= \frac{1}{2}(9n^2 - 3 n + 2) + \frac{9}{2}n(n-1)(n-2) \text{,} \end{align*} and \begin{align*} f(4,n) &= 1 + \sum_{k=1}^4 3^k \binom{n}{k} \\ &= 1 + 3^1 \binom{n}{1} + 3^2 \binom{n}{2} + 3^3 \binom{n}{3} + 3^4 \binom{n}{4} \\ &= 1 + 3 n + 9\frac{n(n-1)}{2} + 27\frac{n(n-1)(n-2)}{6} + 81\frac{n(n-1)(n-2)(n-3)}{24} \\ &= \frac{1}{2}(2 + 6n + 9n(n-1) + 9n(n-1)(n-2)) + \frac{27}{8}n(n-1)(n-2)(n-3) \\ &= \frac{1}{2}(9 n^3 - 18 n^2 + 15 n + 2) + \frac{27}{8}n(n-1)(n-2)(n-3) \text{.} \\ \end{align*}