You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义起始值的广义斐波那契数列第n项快速求解方法求助

Fast Solution for Custom Fibonacci Sequence (O(log n) Time)

Hey there! Let's break down how to solve this problem efficiently—since n can be as big as 1e9+, slow O(n) or recursive methods just won't cut it.

Problem Recap

You need to compute the nth term of a sequence defined by:

  • Base cases: F[0] = a, F[1] = b (where a, b < 101)
  • Recurrence: F[n] = F[n-1] + F[n-2] for n >= 2
  • Result must be modulo 10^M (with M < 8, so mod values range from 10 up to 10^7)

Why Slow Methods Fail

  • Recursive approach: Has an exponential time complexity (O(2^n)) because it recalculates the same terms millions of times. Even n=30 would trigger over a million operations—imagine n=1e9!
  • Iterative DP (O(n)): Looping from 2 to n would take years to finish for n=1e9. It's completely impractical for such large values.

The O(log n) Solution: Matrix Exponentiation

Linear recurrence relations like this can be represented with matrix multiplication, and we can compute large matrix powers in O(log n) time using exponentiation by squaring. This lets us jump directly to the nth term without calculating every intermediate value.

Recurrence as Matrix Multiplication

For your custom sequence, we can write the recurrence as a matrix operation:

[ F(n)   ]   = [ 1 1 ]^(n-1) * [ F(1) ]
[ F(n-1) ]     [ 1 0 ]          [ F(0) ]
  • The matrix [[1,1],[1,0]] is the core transformation for Fibonacci-like sequences.
  • Raising this matrix to the (n-1)th power skips all intermediate steps.
  • We apply modulo 10^M at every step to keep numbers small and avoid overflow.

Example Code (Python)

Python is beginner-friendly and handles big integers well, but we still use modulo to speed up calculations:

def multiply(mat1, mat2, mod):
    """Multiply two 2x2 matrices, return result modulo mod"""
    a = (mat1[0][0] * mat2[0][0] + mat1[0][1] * mat2[1][0]) % mod
    b = (mat1[0][0] * mat2[0][1] + mat1[0][1] * mat2[1][1]) % mod
    c = (mat1[1][0] * mat2[0][0] + mat1[1][1] * mat2[1][0]) % mod
    d = (mat1[1][0] * mat2[0][1] + mat1[1][1] * mat2[1][1]) % mod
    return [[a, b], [c, d]]

def matrix_pow(mat, power, mod):
    """Compute mat^power using exponentiation by squaring, modulo mod"""
    # Start with identity matrix (equivalent to 1 for scalar multiplication)
    result = [[1, 0], [0, 1]]
    while power > 0:
        # If power is odd, multiply result by current matrix
        if power % 2 == 1:
            result = multiply(result, mat, mod)
        # Square the matrix and halve the power
        mat = multiply(mat, mat, mod)
        power = power // 2
    return result

def custom_fib(n, a, b, M):
    mod = 10 ** M
    if n == 0:
        return a % mod
    if n == 1:
        return b % mod
    # Transformation matrix for our sequence
    trans_mat = [[1, 1], [1, 0]]
    # Compute the matrix raised to (n-1)th power
    mat_pow = matrix_pow(trans_mat, n-1, mod)
    # Calculate F(n) using the matrix result
    return (mat_pow[0][0] * b + mat_pow[0][1] * a) % mod

# Test case: a=25, b=60, n=5, M=2 (mod 100)
# F[0]=25, F[1]=60, F[2]=85, F[3]=145%100=45, F[4]=130%100=30, F[5]=75
print(custom_fib(5, 25, 60, 2))  # Output: 75

Alternative Approach: Linear Combination of Standard Fibonacci

If you want to use the fast Fibonacci formula you mentioned, note your custom sequence can be written using the standard Fibonacci sequence (where fib(0)=0, fib(1)=1):

  • F(n) = a * fib(n-1) + b * fib(n)

You could implement the O(log n) formula for the standard Fibonacci sequence, compute fib(n) and fib(n-1), then plug into this equation and take modulo 10^M. However, matrix exponentiation is more straightforward for custom starting values since you don't need to derive this linear combination.

Key Tips

  • Always apply modulo at every arithmetic operation: This prevents overflow (critical in languages like C++/Java) and speeds up calculations in Python.
  • Exponentiation by squaring works because any number can be broken into powers of 2—this cuts the number of multiplications from O(n) to O(log n).

内容的提问来源于stack exchange,提问作者katinas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:10:31