2255: Fibonacci sequence (modulo operation in number theory)

2255: Fibonacci Sequence

Time Limit: 1 Sec   Memory Limit: 128 MB
Commits: 139   Resolved: 64
[ Commit ][ Status ][ Discussion Board ][Proposer: 541507010105 ]

Topic description

The recursive formula of the Fibonacci sequence is: Fn=Fn-1+Fn-2, where F1=F2=1.
When n is relatively large, Fn is also very large, and now we want to know, what is the remainder of dividing Fn by 10007.

enter

The input contains an integer n.

Data size and convention

1 <= n <= 1,000,000。

output

Output a line containing an integer representing the remainder of dividing Fn by 10007.

sample input

10

Sample output

55

hint

In this question, the answer is to ask for the remainder of dividing Fn by 10007, so we only need to be able to calculate the remainder without first calculating the exact value of Fn, and then dividing the calculated result by 10007 to take the remainder. Easier than calculating the original number first and then taking the remainder.

 

#include <iostream>
#include <cstring>

using namespace std ; 

#define maxn 1100000 int fib[maxn] ; int main(){ int n ; cin >> n ; memset(fib , 0 , sizeof(fib)) ; fib[1] = fib[2]= 1 ; for(int i=3 ; i<=n ; i++){ fib[i] = (fib[i-1] + fib[i-2])%10007 ; } cout<<fib[n]<<endl ; return 0 ; }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324663871&siteId=291194637