题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5015

  看到这个限时,我就知道这题不简单~~矩阵快速幂,找递推关系

我们假设第一列为:

23

a1

a2

a3

a4

则第二列为:

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

进一步转化可以得到:

代码:

#include <iostream>
#include <string.h>
using namespace std;
#define N 15
#define mod 10000007 typedef long long LL; int n;
class Matrix{
public:
LL mat[N][N];
Matrix() {
for (int i = ; i < N; i++) {
memset(mat[i], , sizeof(mat[i]));
}
}
Matrix operator*(Matrix b){
Matrix temp;
for (int i = ; i <= n + ; i++){
for (int j = ; j <= n + ; j++){
for (int k = ; k <= n + ; k++){
if (mat[i][k] && b.mat[k][j]){
temp.mat[i][j] = (temp.mat[i][j] + (mat[i][k] % mod * b.mat[k][j] % mod) % mod) % mod;
}
}
}
}
return temp;
}
}; void MatrixMulti(Matrix &M,int m){
Matrix ans;
for (int i = ; i <= n + ; i++)
ans.mat[i][i] = ;
while (m){
if (m & )
ans = ans * M;
m >>= ;
M = M * M;
}
M = ans;
}
Matrix initialize(){
Matrix M;
for (int i = ; i <= n; i++) {
for (int j = ; j <= n + ; j++) {
if (j == )M.mat[i][j] = ;
else if (i >= j)M.mat[i][j] = ;
else if (j == n + )M.mat[i][j] = ;
else M.mat[i][j] = ;
}
}
M.mat[n + ][n + ] = ;
return M;
}
int main(){
int i, m;
while (~scanf("%d%d", &n, &m)){
int a[N];a[] = ;a[n + ] = ;
for (int i = ; i <= n; i++)
scanf("%d", &a[i]);
Matrix M = initialize();
MatrixMulti(M, m);
LL res = ;
for (i = ; i <= n + ; i++)
res = (res + (M.mat[n][i] % mod * a[i] % mod) % mod) % mod;
cout << res << endl;
}
return ;
}

本来是不想直接 mat[N] [N]这样直接开个大数组,时间空间上都浪费,但是这题用指针写起来真的很麻烦!!!而且不知道哪儿写错了,好一会儿我也找不到 -_-,就换成上面这个了。

#include <iostream>
#include <stdio.h>
using namespace std; typedef long long ll;
#define mod 10000007 int** initialize(int *A,int n) {
A[] = ;
for (int i = ; i <= n; i++) {
cin >> A[i];
}
A[n + ] = ;
int **M = new int*[n + ];
for (int i = ; i <= n + ; i++) {
M[i] = new int[n + ];
for (int j = ; j <= n + ; j++) {
if (j == )
M[i][j] = ;
else if (j <= i)M[i][j] = ;
else if (j == n + )M[i][j] = ;
else M[i][j] = ;
}
}
for (int i = ; i <= n + ; i++) {
if (i == n + )M[n + ][i] = ;
else M[n + ][i] = ;
}
return M;
}
void MatrixMulti(int **M1,int **M2,int n,int **M) {
int t[][] = { };
for (int i = ; i <= n + ; i++) {
for (int j = ; j <= n + ; j++) {
for (int k = ; k <= n + ; k++) {
t[i][j] = (t[i][j] + M1[i][k] % mod * M2[k][j] % mod) % mod;
}
}
}
for (int i = ; i <= n + ; i++) {
for (int j = ; j <= n + ; j++) {
M[i][j] = t[i][j];
}
} }
int **getBasicMatrix(int n) {
int **m = new int*[n + ];
for (int i = ; i <= n + ; i++) {
m[i] = new int[n + ];
for (int j = ; j <= n + ; j++) {
if (i == j)m[i][j] = ;
else m[i][j] = ;
}
}
return m;
}
int** MatrixQuickPower(int **M, int n, int m) {
int **res = getBasicMatrix(n);
while (m) {
if (m & )
MatrixMulti(res, M, n, res);
MatrixMulti(M, M, n, M);
m >>= ;
}
return res;
}
int main() {
int n, m;
while (~scanf("%d%d", &n, &m)) {
int res = ;
int *A = new int[n + ];
int **M = initialize(A, n);
M = MatrixQuickPower(M, n, m);
for (int i = ; i <= n + ; i++) {
res = (res + M[n][i] % mod * A[i] % mod) % mod;
}
cout << res << endl;
}
return ;
}

随手练——HDU 5015 矩阵快速幂的更多相关文章

  1. hdu 5015 矩阵快速幂(可用作模板)

    转载:http://blog.csdn.net/wdcjdtc/article/details/39318847 之前各种犯傻 推了好久这个东西.. 后来灵关一闪  就搞定了.. 矩阵的题目,就是构造 ...

  2. hdu 5015(矩阵快速幂z )

    a[i][j] = a[i-1][j] + a[i][j-1] m.特别大,可以计算出第一列,找出规律,构建一个特殊的矩阵,运用快速幂 设矩阵x: 1 0 0 0 ... |10 1 1 1 0 0 ...

  3. HDU 2855 (矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2855 题目大意:求$S(n)=\sum_{k=0}^{n}C_{n}^{k}Fibonacci(k)$ ...

  4. HDU 4471 矩阵快速幂 Homework

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4471 解题思路,矩阵快速幂····特殊点特殊处理····· 令h为计算某个数最多须知前h个数,于是写 ...

  5. HDU - 1575——矩阵快速幂问题

    HDU - 1575 题目: A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973.  Input数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n( ...

  6. hdu 1757 (矩阵快速幂) 一个简单的问题 一个简单的开始

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题意不难理解,当x小于10的时候,数列f(x)=x,当x大于等于10的时候f(x) = a0 * ...

  7. HDU 3802 矩阵快速幂 化简递推式子 加一点点二次剩余知识

    求$G(a,b,n,p) = (a^{\frac {p-1}{2}}+1)(b^{\frac{p-1}{2}}+1)[(\sqrt{a} + \sqrt{b})^{2F_n} + (\sqrt{a} ...

  8. How many ways?? HDU - 2157 矩阵快速幂

    题目描述 春天到了, HDU校园里开满了花, 姹紫嫣红, 非常美丽. 葱头是个爱花的人, 看着校花校草竞相开放, 漫步校园, 心情也变得舒畅. 为了多看看这迷人的校园, 葱头决定, 每次上课都走不同的 ...

  9. HDU 5950 矩阵快速幂

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

随机推荐

  1. 【转】类找不到总结java.lang.ClassNotFoundException

    (1)org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.microsoft.sqls ...

  2. Jprofiler注册码

    L-Larry_Lau@163.com#23874-hrwpdp1sh1wrn#0620 L-Larry_Lau@163.com#36573-fdkscp15axjj6#25257 L-Larry_L ...

  3. CSS starts

    I have not written any articles here since I graduated from my university. Now I begin to write down ...

  4. java设计模式-----16、解释器模式

    概念: Interpreter模式也叫解释器模式,是行为模式之一,它是一种特殊的设计模式,它建立一个解释器,对于特定的计算机程序设计语言,用来解释预先定义的文法.简单地说,Interpreter模式是 ...

  5. Android的onCreateOptionsMenu()创建菜单Menu

    android一共有三种形式的菜单:             1.选项菜单(optinosMenu)             2.上下文菜单(ContextMenu)             3.子菜 ...

  6. 前端构建工具 Gulp.js 上手实例

    在软件开发中使用自动化构建工具的好处是显而易见的.通过工具自动化运行大量单调乏味.重复性的任务,比如图像压缩.文件合并.代码压缩.单元测试等等,可以为开发者节约大量的时间,使我们能够专注于真正重要的. ...

  7. 11.Spring——JDBC框架

    1.DBC 框架概述 2.Spring JDBC 示例 3.Spring 中 SQL 的存储过程 1.DBC 框架概述 在使用普通的 JDBC 数据库时,就会很麻烦的写不必要的代码来处理异常,打开和关 ...

  8. 从AggregateException看异常类的设计

    异常是程序在有bug时最直观的表现形式,不担心有bug存在,而担心bug埋没在大堆的代码中而发现不了. 这篇随笔简单谈谈从AggregateException类源码(http://www.projky ...

  9. 数据契约(DataContract)里的DataMember特性

      数据契约(DataContract) 服务契约定义了远程访问对象和可供调用的方法,数据契约则是服务端和客户端之间要传送的自定义数据类型. 一旦声明一个类型为DataContract,那么该类型就可 ...

  10. Oracle EBS 计划请求

    SELECT fcp.concurrent_program_name, decode(fcre.description, NULL, fcpt.user_concurrent_program_name ...