天天看点

Modular Fibonacci

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=30506#problem/U

fib数列对2^m取模的循环节为3*(2^(m-1))

#include<map>
#include<set>
#include<list>
#include<cmath>
#include<ctime>
#include<deque>
#include<stack>
#include<bitset>
#include<cstdio>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<numeric>
#include<sstream>
#include<utility>
#include<iostream>
#include<algorithm>
#include<functional>

using namespace std ;
const int maxn = 1000005 ;

long long f[ 25 ][ maxn ] ;

void Union()
{
	for( int i = 1 ; i <= 20 ; ++i )
	{
		f[ i ][ 0 ] = 0 ;
		f[ i ][ 1 ] = 1 ;
		long long temp = 1 << i ;
		for( long long j = 2 ; j < 3 * ( 1 << ( i - 1 ) ) ; ++j )
		{
			f[ i ][ j ] = ( f[ i ][ j - 1 ] % temp + f[ i ][ j - 2 ] % temp ) % temp ;
		}
	}
} 

int main()
{
	Union() ;
	int m , n ;
	while( scanf( "%d%d" , &n , &m ) != EOF )
	{
		if( !m )
			printf( "0\n" ) ;
		else
			printf( "%lld\n" , f[ m ][ n % ( 3 * ( 1 << ( m - 1 ) ) ) ] ) ;
	}
    return 0;
}