斐波那契数列对10007取余数

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

因为
*F(n) = X1 * 10007 + Mn
F(n+1) = X2 * 10007 +Mn+1
F(n+2) = F(n) + F(n+1)
=(X1 + X2)+100007 + Mn + Mn+1
Mn+2 = F(n+2) / 10007 = Mn + Mn+1
所以 Mn = ( Mn-1 + Mn-2 ) % 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;
}

发布了1 篇原创文章 · 获赞 0 · 访问量 11

猜你喜欢

转载自blog.csdn.net/qq_15757015/article/details/104089657
今日推荐