hdu 1757 A Simple Math Problem
http://acm.hdu.edu.cn/showproblem.php?pid=1757
如题,简单的矩阵快速幂。1y!
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <cstring> 3 #include <cassert> 4 #include <algorithm> 5 6 using namespace std; 7 8 const int maxSize = 10; 9 const int initMod = 1E9 + 7; 10 int curSize = maxSize; 11 int curMod = initMod; 12 13 struct Matrix { 14 int val[maxSize][maxSize]; 15 16 Matrix(bool ONE = false) { 17 for (int i = 0; i < curSize; i++) { 18 for (int j = 0; j < curSize; j++) { 19 val[i][j] = 0; 20 } 21 if (ONE) val[i][i] = 1; 22 } 23 } 24 25 void print(int _l = curSize, int _w = curSize) { 26 for (int i = 0; i < _l; i++) { 27 for (int j = 0; j < _w; j++) { 28 if (j) putchar(' '); 29 printf("%d", val[i][j]); 30 } 31 puts(""); 32 } 33 puts("~~"); 34 } 35 }; 36 37 Matrix operator * (Matrix &_a, Matrix &_b) { 38 Matrix ret = Matrix(); 39 40 for (int i = 0; i < curSize; i++) { 41 for (int k = 0; k < curSize; k++) { 42 if (_a.val[i][k]) { 43 for (int j = 0; j < curSize; j++) { 44 ret.val[i][j] += _a.val[i][k] * _b.val[k][j]; 45 ret.val[i][j] %= curMod; 46 } 47 } 48 } 49 } 50 51 return ret; 52 } 53 54 Matrix operator ^ (Matrix &_a, int _p) { 55 Matrix __a = _a, ret = Matrix(true); 56 57 while (_p) { 58 if (_p & 1) { 59 ret = ret * __a; 60 } 61 __a = __a * __a; 62 _p >>= 1; 63 } 64 65 return ret; 66 } 67 68 int deal(int k) { 69 if (k < 10) return k % curMod; 70 71 Matrix ans = Matrix(), op = Matrix(); 72 73 for (int i = 0; i < 10; i++) { 74 ans.val[0][i] = 9 - i; 75 } 76 // ans.print(); 77 for (int i = 0; i < 10; i++) { 78 scanf("%d", &op.val[i][0]); 79 } 80 for (int i = 1; i < 10; i++) { 81 op.val[i - 1][i] = 1; 82 } 83 // op.print(); 84 op = op ^ (k - 9); 85 ans = ans * op; 86 // ans.print(); 87 88 return ans.val[0][0]; 89 } 90 91 int main() { 92 int n; 93 94 while (~scanf("%d%d", &n, &curMod)) { 95 printf("%d\n", deal(n)); 96 } 97 98 return 0; 99 }
——written by Lyon