标签:
HDU 5171 GTY‘s birthday gift ( 矩阵快速幂裸题目 )
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define MAX_SIZE 3 #define MOD 10000007 #define clr( a, b ) memset( a, b, sizeof(a) ) typedef long long LL; struct Mat { int n; LL mat[MAX_SIZE][MAX_SIZE]; Mat( int _n ) { n = _n; clr( mat, 0 ); } void init() { for( int i = 0; i < n; ++i ) for( int j = 0; j < n; ++j ) mat[i][j] = ( i == j ); } Mat operator * ( const Mat &b ) const { Mat c( b.n ); for( int k = 0; k < n; ++k ) for( int i = 0; i < n; ++i ) if( mat[i][k] ) for( int j = 0; j < n; ++j ) c.mat[i][j] = ( c.mat[i][j] + mat[i][k] * b.mat[k][j] ) % MOD; return c; } }; Mat fast_mod( Mat a, int b ) { Mat res( a.n ); res.init(); while( b ) { if( b & 1 ) res = res * a; a = a * a; b >>= 1; } return res; } void scan( int &x ) { char c; while( c = getchar(), c < ‘0‘ || c > ‘9‘ ); x = c - ‘0‘; while( c = getchar(), c >= ‘0‘ && c <= ‘9‘ ) x = x *10 + c - ‘0‘; } int n, k, x[ 100001 ], id; int first, second; LL sum, c[ 3 ], res; int main() { Mat M( 3 ); M.mat[0][0] = M.mat[0][2] = M.mat[1][2] = M.mat[2][1] = M.mat[2][2] = 1; while( ~scanf( "%d %d", &n, &k ) ) { sum = 0; first = second = -1; for( int i = 0; i < n; ++i ) { scan( x[i] ); sum += x[i]; if( sum > MOD ) sum -= MOD; if( x[i] > first ) { first = x[i]; id = i; } } for( int i = 0; i < n; ++i ) { if( i == id ) continue; if( x[i] > second ) second = x[i]; } c[ 0 ] = sum - first; c[ 1 ] = second; c[ 2 ] = first; // printf( "c[0] = %lld c[1] = %lld c[2] = %lld\n", c[0],c[1],c[2] ); Mat b = fast_mod( M, k + 1 ); res = ( c[ 0 ] * b.mat[0][0] + c[ 1 ] * b.mat[0][1] + c[ 2 ] * b.mat[0][2] ) % MOD ; printf( "%I64d\n", res ); } return 0; }
HDU 5171 GTY's birthday gift(矩阵快速幂 )
标签:
原文地址:http://www.cnblogs.com/BigBallon/p/4293129.html