Blue Bridge Cup series entry --1.Fibonacci

Problem description
the Fibonacci recursion formula: Fn = Fn-1 + Fn -2, where F1 = F2 = 1.

When n is relatively large, Fn is very great, and now we want to know, Fn is divided by the number 10007 is.

Input format
input contains an integer n.
Output format
output one line containing an integer representing the Fn remainder of the division of 10007.
Description: In this problem, the answer is to require Fn is divided by 10007, so we can figure this out as long as the remainder can be, without the need to calculate the exact value of Fn, then the result is calculated by dividing the number 10007 to take over direct calculation the remainder often than first calculate the original number and then take the remainder simple.

Input Sample
10
Sample output
55
Sample Input
22
sample output
7704
data size and Conventions
1 <= n <= 1,000,000.

Note that this question can not be recursive solution, easy data overflow, we take the remainder using a loop, and then solved

#include <iostream>

using namespace std;

int main()
{
    int n;int p1=1,p2=1,p3=1;
    cin>>n;
    if(n!=1&&n!=2)
        for(int i=3;i<=n;i++){
            p3=(p1+p2)%10007;
            p1=p2;
            p2=p3;
        }
    cout<<p3;
    return 0;
}

Guess you like

Origin www.cnblogs.com/littlepage/p/11690911.html