hdu3117-Fibonacci Numbers

来源:互联网 发布:网络层 ip层 编辑:程序博客网 时间:2024/05/17 07:13

http://acm.hdu.edu.cn/showproblem.php?pid=3117

后四位存在周期15000,所以就打表打出了后四位的值。

前四位按照Fibonacci的通项公式取前四位即可

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>using namespace std;int fi[ 100 ] = { 0 , 1 , 1 } ;int Last[ 15005 ] ;int main(){int n ;Last[ 0 ] = 0 , Last[ 1 ] = 1 ; for( int i  = 2 ; i <= 15000 ; i++ )    Last[ i ] = ( Last[ i - 1 ] + Last[ i - 2 ] ) % 10000 ;for( int i = 2 ; i < 40 ; ++i )fi[ i ] = fi[ i - 1] + fi[ i - 2 ] ;while( cin >> n ) {if( n < 40 ){cout << fi[ n ] << endl ;}else{double temp = -0.5 * log10(5.0) + (n ) * log10((sqrt(5.0) + 1.0 ) /2.0 ) ;temp -= floor( temp ) ;temp = pow( 10.0 , temp ) ;while( temp < 1000 )temp *= 10 ;/*temp = (int)temp ;cout << temp << "..." << Last[ n % 15000 ] <<  endl ;*/printf( "%d..." , ( int ) temp ) ;           printf( "%04d\n" , Last[ n % 15000 ] ) ;}}return 0 ;}