Fibonacci number to take the remainder 10007

Mn = (Mn-1 + Mn-2)% 10007

因为
* F (N) = x1 * 10007 + Mn is
F (N + 1) = X2 is * 10007 + Mn is + 1
F (N + 2) = F (N) + F (N + 1)
= (x1 + X2 is ) +100,007 + Mn is + Mn is + 1
Mn is + 2 = F (N + 2) / 10007 = Mn is + Mn is + 1
所以= Mn is (1-Mn is + 2-Mn is )% 10007

#include <iostream>
int main()
{
    int a = 1,b = 0, sum = 0,t = 0, n;
    std::cin >> n;
    while ( ++t <= n )
    {
        sum = ( a + b ) % 10007;
        a = b;
        b = sum;
    }
    std::cout << sum;
    return 0;
}

Published an original article · won praise 0 · Views 11

Guess you like

Origin blog.csdn.net/qq_15757015/article/details/104089657