首页 > 代码库 > Hdu 1757A Simple Math Problem矩阵
Hdu 1757A Simple Math Problem矩阵
构造个矩阵搞下就行了
a0 a1 a2 a3 a4 a5 a6 a7 a8 a9
1 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 1 0
#include <cstdio>#include <algorithm>#include <iostream>#include <string.h>typedef long long LL;using namespace std;LL vis[100];LL M;struct Matrix{ LL m[12][12];};LL n;Matrix Mul(Matrix a, Matrix b){ Matrix ans; for (LL i = 0; i < 10; i++){ for (LL j = 0; j < 10; j++){ ans.m[i][j] = 0; for (LL k = 0; k < 10; k++){ ans.m[i][j] += a.m[i][k] * b.m[k][j]; ans.m[i][j] %= M; } } } return ans;}Matrix quick(Matrix a, LL b){ Matrix ans; for (LL i = 0; i < 10; i++) for (LL j = 0; j < 10; j++) ans.m[i][j] = (i == j); while (b){ if (b & 1) ans = Mul(ans, a); a = Mul(a, a); b >>= 1; } return ans;}Matrix init(){ for (LL i = 0; i < 10; i++) scanf("%d", &vis[i]); Matrix ans; for (LL i = 0; i < 10; i++) for (LL j = 0; j < 10; j++) ans.m[i][j] = 0; for (LL i = 0; i < 10; i++) ans.m[0][i] = vis[i]; for (LL i = 1; i < 10; i++){ ans.m[i][i - 1] = 1; } return ans;}void gao(){ LL t = n - 9; Matrix ans = init(); ans = quick(ans, t); LL sum = 0; for (LL i = 0; i < 10; i++){ sum += ans.m[0][i] * (9 - i); sum %= M; } cout << sum << endl;}int main(){ while (cin >> n >> M){ if (n < 10){ cout << n << endl; continue; } gao(); } return 0;}
Hdu 1757A Simple Math Problem矩阵
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。