目录
题意: 有一种矩阵,它的第一行是这样一些数:a 0,0 = 0, a 0,1 = 233,a 0,2 = 2333,a 0,3 = 23333...) 除此之外,在这个矩阵里, 我们有 a i,j = a i-1,j +a i,j-1( i,j ≠ 0).现在给你 a 1,0,a 2,0,...,a n,0,求a n,m 是多少。
析:把这个矩阵构造成n+2 * n+2 的
矩阵快速幂,构造出矩阵即可。
第一列元素为:
0
a1
a2
a3
a4
转化为:
23
a1
a2
a3
a4
3
则第二列为:
23*10+3
23*10+3+a1
23*10+3+a1+a2
23*10+3+a1+a2+a3
23*10+3+a1+a2+a3+a4
3
就形成了矩阵:
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #define debug() puts("++++"); #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair< int , int > P; const int INF = 0x3f3f3f3f; const LL LNF = 1e17; const double inf = 0x3f3f3f3f3f3f; const double PI = acos (-1.0); const double eps = 1e-8; const int maxn = 20 + 10; const int mod = 10000007; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; const char *de[] = { "0000" , "0001" , "0010" , "0011" , "0100" , "0101" , "0110" , "0111" , "1000" , "1001" , "1010" , "1011" , "1100" , "1101" , "1110" , "1111" }; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in( int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } struct Matrix{ int a[12][12]; Matrix(){ memset (a, 0, sizeof a); } void init(){ for ( int i = 0; i < n; ++i) a[i][i] = 1; } friend Matrix operator * ( const Matrix &lhs, const Matrix &rhs){ Matrix res; for ( int i = 0; i < n; ++i) for ( int j = 0; j < n; ++j) for ( int k = 0; k < n; ++k) res.a[i][j] = (lhs.a[i][k] * (LL)rhs.a[k][j] + res.a[i][j]) % mod; return res; } }; Matrix fast_pow(Matrix a, int n){ Matrix res; res.init(); while (n){ if (n & 1) res = res * a; n >>= 1; a = a * a; } return res; } int main(){ while ( scanf ( "%d %d" , &n, &m) == 2){ Matrix first; first.a[0][0] = 23; for ( int i = 1; i <= n; ++i){ scanf ( "%d" , &first.a[i][0]); first.a[i][0] %= mod; } first.a[n+1][0] = 3; n += 2; Matrix second; for ( int i = 0; i + 1 < n; ++i){ second.a[i][0] = 10; for ( int j = 0; j < i; ++j) second.a[i][j+1] = 1; second.a[i][n-1] = 1; } second.a[n-1][n-1] = 1; Matrix ans = fast_pow(second, m) * first; printf ( "%d\n" , ans.a[n-2][0]); } return 0; } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步