Блог пользователя xuanquang1999

Автор xuanquang1999, 9 лет назад, По-английски

Is there a way to do this?

  • Проголосовать: нравится
  • +11
  • Проголосовать: не нравится

»
9 лет назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

Yes, there is.

»
9 лет назад, # |
Rev. 10   Проголосовать: нравится +21 Проголосовать: не нравится

Yes, there is. Think about the formula...

Fi = Fi - 1 + Fi - 2 = 2 * Fi - 2 + Fi - 3 = 3 * Fi - 3 + 2 * Fi - 4....

If you keep on with the formula, assuming F0 = 1 and F1 = 1, you'll reach a point where...

(if i is even)

(if i is odd)

Or, generalizing for every i...

Using those formulas and a recursive function with memoization, you can run the algorithm in no time.

  • »
    »
    9 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    The implementation is very short and simple. Check it out here -> Fibonacci

  • »
    »
    9 лет назад, # ^ |
      Проголосовать: нравится +5 Проголосовать: не нравится

    That is interesting.

    Is this for Fibonacchi Numbers only?

    • »
      »
      »
      9 лет назад, # ^ |
      Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

      What do you mean? It's a function that calculates the nth Fibonacci number in O(logn).

      • »
        »
        »
        »
        9 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        Generally we do these things by matrix exponentiation.

        What I meant to ask is whether it is possible for all such problems to find some formula and solve it. Like you did.

        • »
          »
          »
          »
          »
          9 лет назад, # ^ |
            Проголосовать: нравится -7 Проголосовать: не нравится

          Ahhhhh, I see. I don't know about matrix exponentiation. I always found it very hard and never decided to actually study about it.

          I knew Fibonacci numbers could be calculated using matrix exponentiation, but since that's beyond my understanding, I decided to try and find my own method. I did, and I must say I find this method much easier to understand, easier to code and maybe even faster.

          I don't know if there's a general formula for other problems that use matrix exponentiation too. I think that there might some that can be solved by a formula and also many others that can't.

        • »
          »
          »
          »
          »
          9 лет назад, # ^ |
            Проголосовать: нравится +16 Проголосовать: не нравится

          If you can formulate your number like , you can solve it using matrix exponentiation. This is a nice article about it!

          • »
            »
            »
            »
            »
            »
            9 лет назад, # ^ |
              Проголосовать: нравится +16 Проголосовать: не нравится

            Thanks for mentioning my blog post! I almost forgot that such post exists :P

        • »
          »
          »
          »
          »
          9 лет назад, # ^ |
          Rev. 2   Проголосовать: нравится -11 Проголосовать: не нравится

          Always,when you can present your sequence member using lower grade sequence numbers,you can calculate it in same way,as describe tenshi_kanade

          for example: if F(n)=F(n/3)*F(n/3+1)+x you can calculate in O(2^log(3,n))

  • »
    »
    9 лет назад, # ^ |
      Проголосовать: нравится +5 Проголосовать: не нравится

    I instantly thought of Matrix Exponentiation when I read the statement...

    But this method is even easier and prettier... m(_ _)m

»
9 лет назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

By the way can anyone say how to calculate Pisano period fast?

  • »
    »
    9 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    There is formulae for prime powers. So you need only factorize modulo.

    • »
      »
      »
      9 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      What formulae?

      • »
        »
        »
        »
        9 лет назад, # ^ |
        Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

        For p=5^k pisano period is a divisor of 5^k; For p quadratic residue modulo 5 pisano period is a divisor of (p-1)p^{k-1}; For p quadratic nonresidue modulo 5 pisano period is a divisor of 2(p+1)p^{k-1}

  • »
    »
    9 месяцев назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    i am searching for pisano period (1e9 + 7) any idea?

    • »
      »
      »
      9 месяцев назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      $$$2e9 + 16$$$

    • »
      »
      »
      9 месяцев назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      As zeliboba mentioned above, the Pisano period is a divisor of $$$2(p+1)$$$, since $$$p=10^9+7$$$ is not a quadratic residue modulo $$$5$$$. Then, you can use standard algorithms for finding a multiplicative order of a number. Specifically, to check if the period is a divisor of $$$k$$$, you check that $$$F_k=F_0$$$ and $$$F_{k+1} = F_1$$$.

»
9 лет назад, # |
Rev. 6   Проголосовать: нравится +1 Проголосовать: не нравится

I'm not sure about this, but you may test it. The formula for the n-th Fibonacci number is

If we write the pow function to be fast modulo exponentiation this gives us O(logN) complexity.

UPD: This function might not work with double arguments, so this is hardly a right solution.

  • »
    »
    9 лет назад, # ^ |
    Rev. 3   Проголосовать: нравится +5 Проголосовать: не нравится

    This formula is applicable iff 5 is a quadratic residue modulo p and p is prime. We can check it using Euler's criterion.

    For example, let's take p = 109 + 9. Euler's criterion is true: . That means we can find a square root of 5 modulo p.

    Let's find it. I prefer Wolfram Alpha: powermod[5,1/2,10^9+9] = 383008016. We can insert this number into your formula, and the final result is

    Fn = 276601605·(691504013n - 308495997n)

    • »
      »
      »
      9 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      So this actually works?

    • »
      »
      »
      9 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Still can't understand where did you get all the numbers in the formula.

      • »
        »
        »
        »
        8 лет назад, # ^ |
        Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

        They do this by finding some number x such that . This is like the in . It turns out that for p = 109 + 9, this x actually exists and is x = 383008016, which you can find out using Wolfram Alpha. They then substitute 383008016 for in your formula and simplify to get those numbers.

    • »
      »
      »
      9 лет назад, # ^ |
      Rev. 12   Проголосовать: нравится +21 Проголосовать: не нравится

      If I am not mistaken, you can do same thing for all modules not equal to 5 by adding roots of x2 - 5 to if 5 is not quadratic residue . I mean, calculate value of the same formula on numbers of type a + bx, where and with rules of addition and multiplication similar to complex numbers (result will always be free of "imaginary" part):

      • »
        »
        »
        »
        9 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        I guess you are right. The thing is the expression never contains square root of five. So we don't really care whether it even exists. It is nice if it does, but it is totally unnecessary. For example my program (link) can calculate Fibonacci numbers modulo 3^6*7^2=35721. And there is no x such that x*x=5 mod 35721. The trick is to work with numbers, similar to complex, with only one difference that i*i=5, not -1.

  • »
    »
    9 лет назад, # ^ |
      Проголосовать: нравится +13 Проголосовать: не нравится

    Nevertheless you can actually use this formula to calculate Fibonacci numbers modulo any odd number. You just need to calculate it not with the help of doubles, but numbers of the form . They have properties similar to complex numbers. For example the product of two numbers equals .

    And because of special structure of this formula, the answer is , where Im takes the part right next to square root of five (I called it so because of similarity with complex numbers). So you only need to be able to divide by two and that can be done modulo any odd number.

»
9 лет назад, # |
  Проголосовать: нравится +15 Проголосовать: не нравится
»
9 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Yes. See this page.

»
9 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

You came and gave a problem: Calculate Fib(n)=Fib(n-1)+Fib(n-2) % M in O(logN). Cool.

Now I come and give you a challenge: SuperFib(n)=A*SuperFib(n-1)+B*SuperFib(n-2)+C*SuperFib(n-3) % M (SuperFib(0), SF(1), SF(2) are given, as well as A, B and C). Solve it in O(logN) time. This requires matrix exponential. If you want to learn something great.

»
7 лет назад, # |
  Проголосовать: нравится -34 Проголосовать: не нравится

You could use matrix multiplication here, to get to the Nth number in log(N) time. Here is a video tutorial explaining the same: Fibonacci Numbers using Matrix Multiplication

»
9 месяцев назад, # |
Rev. 2   Проголосовать: нравится -74 Проголосовать: не нравится

.

»
9 месяцев назад, # |
Rev. 2   Проголосовать: нравится -20 Проголосовать: не нравится

.

»
9 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

idk