斐波那契数列 递归超时问题

斐波那契数列

在进行算法运算时,使用递归运算,运算速度较慢。根据要求用数组存储比较快。


这次的题目中,是直接计算其余数。

题目:

Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1。

当n比较大时,Fn也非常大,现在我们想知道,Fn除以10007的余数是多少。

  1. #include    
  2. #define M 10007    
  3. int main()    
  4. {    
  5.     int a1,a2;    
  6.     a1=a2=1;    
  7.     int temp;  
  8.     long n;    
  9.     long i;    
  10.     scanf("%ld",&n);    
  11.         
  12.     for(i=1;i
  13.     {     
  14.         temp=a2;    
  15.         a2=(a1+a2)%M;    
  16.         a1=temp;     
  17.     }   
  18.     printf("%d\n",a1);    
  19.     return 0;    
  20. }     

你可能感兴趣的:(斐波那契数列 递归超时问题)