HDU 5171 GTY's birthday gift(矩阵快速幂 )

 

 

HDU 5171 GTY's birthday gift ( 矩阵快速幂裸题目 )

HDU 5171 GTY's birthday gift(矩阵快速幂 )

隐藏行号 复制代码 矩阵快速幂
  1. #include <cstdio>    
    
    
  2. #include <cstring>
    
    
  3. #include <algorithm>
    
    
  4. using namespace std;
    
    
  5. #define MAX_SIZE 3
    
    
  6. #define MOD 10000007
    
    
  7. #define clr( a, b ) memset( a, b, sizeof(a) )
    
    
  8. typedef long long LL;
    
    
  9. 
    
    
  10. struct Mat
    
    
  11. {
    
    
  12.     int n;
    
    
  13.     LL mat[MAX_SIZE][MAX_SIZE];
    
    
  14.     Mat( int _n )
    
    
  15.     {
    
    
  16.         n = _n;
    
    
  17.         clr( mat, 0 );
    
    
  18.     }
    
    
  19.     void init()
    
    
  20.     {
    
    
  21.         for( int i = 0; i < n; ++i )
    
    
  22.             for( int j = 0; j < n; ++j )
    
    
  23.                 mat[i][j] = ( i == j );
    
    
  24.     }
    
    
  25.     Mat operator * ( const Mat &b ) const
    
    
  26.     {
    
    
  27.         Mat c( b.n );
    
    
  28.         for( int k = 0; k < n; ++k )
    
    
  29.             for( int i = 0; i < n; ++i )    if( mat[i][k] )
    
    
  30.                 for( int j = 0; j < n; ++j )
    
    
  31.                     c.mat[i][j] = ( c.mat[i][j] + mat[i][k] * b.mat[k][j] ) % MOD;
    
    
  32.         return c;
    
    
  33.     }
    
    
  34. };
    
    
  35. 
    
    
  36. Mat fast_mod( Mat a, int b )
    
    
  37. {
    
    
  38.     Mat res( a.n );
    
    
  39.     res.init();
    
    
  40.     while( b )
    
    
  41.     {
    
    
  42.         if( b & 1 )    res = res * a;
    
    
  43.         a = a * a;
    
    
  44.         b >>= 1; 
    
    
  45.     }
    
    
  46.     return res;
    
    
  47. }
    
    
  48. 
    
    
  49. void scan( int &x )
    
    
  50. {
    
    
  51.     char c;
    
    
  52.     while( c = getchar(), c < '0' || c > '9' );
    
    
  53.     x = c - '0';
    
    
  54.     while( c = getchar(), c >= '0' && c <= '9' ) x = x *10 + c - '0';
    
    
  55. }
    
    
  56. 
    
    
  57. int n, k, x[ 100001 ], id;
    
    
  58. int first, second;
    
    
  59. LL sum, c[ 3 ], res;
    
    
  60. 
    
    
  61. int main()
    
    
  62. {
    
    
  63.     Mat M( 3 );
    
    
  64.     M.mat[0][0] = M.mat[0][2] = M.mat[1][2] = M.mat[2][1] = M.mat[2][2] = 1;
    
    
  65.     while( ~scanf( "%d %d", &n, &k ) )
    
    
  66.     {
    
    
  67.         sum = 0;
    
    
  68.         first = second = -1;
    
    
  69.         for( int i = 0; i < n; ++i )
    
    
  70.         {
    
    
  71.             scan( x[i] );
    
    
  72.             sum += x[i];
    
    
  73.             if( sum > MOD ) sum -= MOD; 
    
    
  74.             if( x[i] > first )
    
    
  75.             {
    
    
  76.                 first = x[i];
    
    
  77.                 id = i;
    
    
  78.             }
    
    
  79.         }
    
    
  80.         for( int i = 0; i < n; ++i )
    
    
  81.         {
    
    
  82.             if( i == id ) continue;
    
    
  83.             if( x[i] > second ) second = x[i];
    
    
  84.         }
    
    
  85.         c[ 0 ] = sum - first;
    
    
  86.         c[ 1 ] = second;
    
    
  87.         c[ 2 ] = first;
    
    
  88.     //    printf( "c[0] = %lld c[1] =  %lld c[2] = %lld\n", c[0],c[1],c[2] );
    
    
  89.         Mat b = fast_mod( M, k + 1 );
    
    
  90.         res = ( c[ 0 ] *  b.mat[0][0] + c[ 1 ] * b.mat[0][1] + c[ 2 ] * b.mat[0][2] ) % MOD ;
    
    
  91.         printf( "%I64d\n", res );
    
    
  92.         
    
    
  93.     }
    
    
  94.     return 0;
    
    
  95. }
    
    

 

你可能感兴趣的:(birt)