Everybody knows Fibonacci numbers, now we are talking about the Tribonacci numbers:
T[0] = T[1] = T[2] = 1;
T[n] = T[n - 1] + T[n - 2] + T[n - 3] (n >= 3)
Given a and b, you are asked to calculate the sum from the ath Fibonacci number to the bth Fibonacci number, mod 1,000,000,007, that is (T[a] + T[a + 1] + ... + T[b]) % 1,000,000,007.
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; ; ; __int64 N; void multipy( __int64 a[MAX_N][MAX_N], __int64 b[MAX_N][MAX_N], __int64 c[MAX_N][MAX_N] ){ ; i <= ; i++ ){ ; j <= ; j++ ){ c[i][j] = ; ; k <= ; k++ ){ c[i][j] = ( c[i][j] + a[i][k] * b[k][j] % MOD ) % MOD; } } } } void get_matrix_pow( __int64 a[MAX_N][MAX_N], __int64 n ){ __int64 ans[MAX_N][MAX_N] = {}; __int64 temp[MAX_N][MAX_N]; ; i <= ; i++ ) ans[i][i] = ; while( n ){ == ){ multipy( ans, a, temp ); memcpy( ans, temp, sizeof( __int64 ) * MAX_N * MAX_N ); } multipy( a, a, temp ); memcpy( a, temp, sizeof( __int64 ) * MAX_N * MAX_N ); n /= ; } memcpy( a, ans, sizeof( __int64 ) * MAX_N * MAX_N ); } __int64 solve( __int64 n ){ __int64 a[MAX_N][MAX_N] = {}; ){ ; } ){ ; } ){ ; } a[][] = ;a[][] = ;a[][] = ;a[][] = ; a[][] = ;a[][] = ;a[][] = ;a[][] = ; a[][] = ;a[][] = ;a[][] = ;a[][] = ; a[][] = ;a[][] = ;a[][] = ;a[][] = ; get_matrix_pow( a, n - ); __int64 ans = ; __int64 b[MAX_N]; b[] = b[] = b[] = ; b[] = ; ; i <= ; i++ ){ ans = ( ans + a[][i] * b[i] % MOD ) % MOD; } return ans; } int main(){ __int64 A, B; while( scanf( "%I64d%I64d", &A, &B ) != EOF ){ printf( ) + MOD ) % MOD ); } ; }