HDU1575_矩阵的入门题目,比较基础。

/*
*State: 1575    0MS    256K    1916 B    C++
*题目大意:
*        A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),
*        现要求Tr(A^k)%9973。
*解题思路:
*        普通矩阵计算即可。
*解题感想:
*        注意该模板为ndim赋值很重要,这个为实际矩阵的大小。
*/
View Code
 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 #define MAX_DIMENSION 11 
 5 typedef int MATRIX_TYPE;
 6 typedef  unsigned __int64 MAX_INT_TYPE; //temporary var to avoid overflowing
 7 typedef MATRIX_TYPE Matrix[MAX_DIMENSION][MAX_DIMENSION];
 8 int ndim = MAX_DIMENSION;
 9 int mod = 9973;//mod
10 
11 void m_zero(Matrix  x)
12 {
13     memset(x, 0, sizeof(MATRIX_TYPE)*MAX_DIMENSION*MAX_DIMENSION);
14 }
15 
16 void m_one(Matrix  x)
17 {
18     int i;
19     m_zero(x);
20     for(i=0;i<ndim;++i)x[i][i]=1;
21 }
22 
23 void m_copy(Matrix  src,Matrix  dest)
24 {
25     memcpy(dest,src, sizeof(MATRIX_TYPE)*MAX_DIMENSION*MAX_DIMENSION);
26 }
27 
28 //c=a*b
29 void m_multiple(Matrix  a,Matrix b,Matrix c)
30 {
31     int i,j,k;
32     MAX_INT_TYPE t;
33 
34     for(i=0;i<ndim;i++)
35         for(j=0;j<ndim;j++)
36         {
37             t=0;
38             if(mod<=1)
39                 for(k=0;k<ndim;k++) t+=a[i][k]*b[k][j];//module
40             else
41                 for(k=0;k<ndim;k++){
42                     t+=(a[i][k]*(MAX_INT_TYPE)b[k][j])%mod;
43                     t%=mod;
44                 }//module
45             c[i][j]=t;
46         }
47 }
48 
49 void m_pow(Matrix x, unsigned int n, Matrix y)
50 {
51     Matrix temp,temp_x;
52     m_one(y);
53     m_copy(x,temp_x);
54     for(;n;)
55     {
56         if ((n & 1) != 0)
57         {
58             m_multiple(temp_x,y,temp);
59             m_copy(temp,y);
60         }
61         if ((n >>= 1))
62         {
63             m_multiple(temp_x,temp_x,temp);
64             m_copy(temp,temp_x);
65         }
66     }
67 }
68 
69 int main(void)
70 {
71 #ifndef ONLINE_JUDGE
72     freopen("in.txt", "r", stdin);
73 #endif
74     int cas;
75     scanf("%d", &cas);
76     while(cas--)
77     {
78         int n, k;
79         scanf("%d %d", &n, &k);
80         Matrix a, ans;
81         ndim = n;
82         for(int i = 0; i < n; i++)
83         {
84             for(int j = 0; j < n; j++)
85                 scanf("%d", &a[i][j]);
86         }
87         m_pow(a, k, ans);
88         int res = 0;
89         for(int i = 0; i < n; i++)
90         {
91             res = (res + ans[i][i]) % mod;
92         }
93         printf("%d\n", res);
94     }
95     return 0;
96 }

HDU1757_矩阵加速递推式产生_递推式都给出了,就比较简单了。

/*
*State: 1757    0MS    304K    2339 B    C++
*题目大意:
*        If x < 10 f(x) = x.
*        If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);
*        And ai(0<=i<=9) can only be 0 or 1 .
*        Now, I will give a0 ~ a9 and two positive integers k and m ,
*        and could you help Lele to caculate f(k)%m.
*解题思路:
*        类似fib那样求即可。
*解题感想:
*        最后的那个矩阵看错了,囧了半小时~是不是走神了?
*/
View Code
  1 #include <stdio.h>
  2 #include <iostream>
  3 #include <string.h>
  4 
  5 #define MAX_DIMENSION 11
  6 
  7 using namespace std;
  8 
  9 typedef int MATRIX_TYPE;
 10 typedef  unsigned __int64 MAX_INT_TYPE; //temporary var to avoid overflowing
 11 typedef MATRIX_TYPE Matrix[MAX_DIMENSION][MAX_DIMENSION];
 12 int ndim = MAX_DIMENSION;
 13 int mod = 9973;//mod
 14 
 15 void m_zero(Matrix  x)
 16 {
 17     memset(x, 0, sizeof(MATRIX_TYPE)*MAX_DIMENSION*MAX_DIMENSION);
 18 }
 19 
 20 void m_one(Matrix  x)
 21 {
 22     int i;
 23     m_zero(x);
 24     for(i=0;i<ndim;++i)x[i][i]=1;
 25 }
 26 
 27 void m_copy(Matrix  src,Matrix  dest)
 28 {
 29     memcpy(dest,src, sizeof(MATRIX_TYPE)*MAX_DIMENSION*MAX_DIMENSION);
 30 }
 31 
 32 //c=a*b
 33 void m_multiple(Matrix  a,Matrix b,Matrix c)
 34 {
 35     int i,j,k;
 36     MAX_INT_TYPE t;
 37 
 38     for(i=0;i<ndim;i++)
 39         for(j=0;j<ndim;j++)
 40         {
 41             t=0;
 42             if(mod<=1)
 43                 for(k=0;k<ndim;k++) t+=a[i][k]*b[k][j];//module
 44             else
 45                 for(k=0;k<ndim;k++){
 46                     t+=(a[i][k]*(MAX_INT_TYPE)b[k][j])%mod;
 47                     t%=mod;
 48                 }//module
 49             c[i][j]=t;
 50         }
 51 }
 52 
 53 void m_pow(Matrix x, unsigned int n, Matrix y)
 54 {
 55     Matrix temp,temp_x;
 56     m_one(y);
 57     m_copy(x,temp_x);
 58     for(;n;)
 59     {
 60         if ((n & 1) != 0)
 61         {
 62             m_multiple(temp_x,y,temp);
 63             m_copy(temp,y);
 64         }
 65         if ((n >>= 1))
 66         {
 67             m_multiple(temp_x,temp_x,temp);
 68             m_copy(temp,temp_x);
 69         }
 70     }
 71 }
 72 
 73 void view_arr(Matrix a, int n)
 74 {
 75     for(int i = 0; i < n; i++)
 76     {    
 77         for(int j = 0; j < n; j++)
 78             cout << a[i][j] << " ";
 79         cout << endl;
 80     }
 81 }
 82 
 83 int main(void)
 84 {
 85 #ifndef ONLINE_JUDGE
 86     //freopen("in.txt", "r", stdin);
 87 #endif
 88 
 89     int k, m;
 90     while(scanf("%d %d", &k, &m) == 2)
 91     {
 92         Matrix a, ans;
 93         m_zero(a);
 94         ndim = 10;
 95         mod = m;
 96 
 97         for(int i = 0; i < 10; i++)
 98             scanf("%d", &a[0][i]);
 99         for(int i = 1; i < 10; i++)
100             a[i][i - 1] = 1;
101         if(k < 10)
102         {
103             printf("%d\n", k % mod);
104             continue;
105         }
106         //view_arr(a, 10);
107         m_pow(a, k - 9, ans);
108         //view_arr(ans, 10);
109 
110         int res = 0;
111         for(int i = 0; i < 10; i++)
112             res = (res + ans[0][i] * (9 - i)) % mod;
113         printf("%d\n", res);
114     }
115     return 0;
116 }

 

posted on 2012-08-02 10:47  cchun  阅读(429)  评论(0编辑  收藏  举报