首页 > 代码库 > Hdu5015 233 Matrix矩阵
Hdu5015 233 Matrix矩阵
每一列能由前一列推过来,构造一个(n+2)*(n+2)的矩阵B,第k列 就等于 所构造的第一列A*B^(k-1)
比如
233 10 10 10 0 2333
a1+233 * 1 1 0 = a1+233+2333
a1+a2+233 1 0 a1+a2+233+a1+233+2333
3 1 1 1 1 3
#include <cstdio>#include <algorithm>#include <iostream>#include <string.h>typedef long long LL;using namespace std;const LL mod = 10000007;LL n;struct Matrix{ LL m[15][15];};Matrix Mul(Matrix a, Matrix b){ Matrix ans; for (LL i = 0; i < n + 2; i++){ for (LL j = 0; j < n + 2; j++){ ans.m[i][j] = 0; for (LL k = 0; k < n + 2; k++){ ans.m[i][j] += a.m[i][k] * b.m[k][j]; ans.m[i][j] %= mod; } } } return ans;}Matrix quick(Matrix a, LL b){ Matrix ans; for (LL i = 0; i < n + 2; i++) for (LL j = 0; j < n + 2; j++) ans.m[i][j] = (i == j); while (b){ if (b & 1) ans = Mul(ans, a); b >>= 1; a = Mul(a, a); } return ans;}Matrix init(){ Matrix ans; for (LL i = 0; i < n + 2; i++) for (LL j = 0; j < n + 2; j++) ans.m[i][j] = 0; for (LL i = 0; i < n + 1; i++) ans.m[0][i] = 10; for (LL i = 1; i < n + 1; i++){ for (LL j = i; j < n + 1; j++) ans.m[i][j] = 1; } for (LL i = 0; i < n + 2; i++) ans.m[n + 1][i] = 1; return ans;}int main(){ LL a[1000]; LL k; while (cin >> n >> k){ for (LL i = 1; i < n + 1; i++) cin >> a[i]; a[0] = 233; a[n + 1] = 3; for (LL i = 1; i<n + 1; i++) a[i] += a[i - 1]; Matrix gao = init(); gao = quick(gao, k - 1); LL sum = 0; for (LL i = 0; i < n + 2; i++){ sum += a[i] * gao.m[i][n]; sum %= mod; } cout << sum << endl; } return 0;}
Hdu5015 233 Matrix矩阵
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。