解题思路:分析需要不少时间,比较懒,直接把别人的分析贴在这里,

  然后贴上自己写的代码:

K相当之大。所以逐一递推的算法无法胜任。这时我们就不得不运用矩阵加速。首先来讲一下矩阵乘法:
若一矩阵的列数与另一矩阵的行数相等,则可定义这两个矩阵的乘积。如 A 是 m×n 矩阵和 B 是 n×p矩阵,它们是乘积 AB 是一个
m×p 矩阵,其中
(AB)[i, j] = A[i, 1] * B[1, j] + A[i, 2] * B[2, j] + ... + A[i, n]
* B[n, j] 对所有 i 及 j。
此乘法有如下性质:
(AB)C = A(BC) 对所有 k×m 矩阵 A, m×n 矩阵 B 及 n×p 矩阵 C ("结合律").
(A + B)C = AC + BC 对所有 m×n 矩阵 A 及 B 和 n×k 矩阵 C ("分配律")。
C(A + B) = CA + CB 对所有 m×n 矩阵 A 及 B 和 k×m 矩阵 C ("分配律")。
要注意的是:可置换性不一定成立,即有矩阵 A 及 B 使得 AB ≠ BA。

下面我们研究一下这道题如何运用矩阵。

f(x) = a0 *
f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10)
构造的矩阵是:

|0
1 0 .........
0|   
|f0|  
|f1 |
|0 0 1 0 .......
0|    |f1|  
|f2 |
|................1| *  |..| =
|...|
|a9 a8
.........a0|   
|f9|  
|f10|

*/

我们看到规律了,每次要到下次个A*B,以此类推则由A*A*A.......A*B;

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = ;
int k, mod; struct MT{
int m[maxn][maxn];
}; MT Mul(MT a, MT b)
{
MT res;
memset(res.m, , sizeof(res.m)); for(int i = ; i < ; i++)
{
for(int j = ; j < ; j++)
{
for(int k = ; k < ; k++)
{
res.m[i][j] += a.m[i][k]*b.m[k][j] % mod;
}
res.m[i][j] %= mod;
}
}
return res;
} MT Product(MT a, int k)
{
MT r;
memset(r.m, , sizeof(r.m));
for(int i = ; i < ; i++)
{
r.m[i][i] = ;
} while(k)
{
if(k & ) r = Mul(r, a);
k >>= ;
a = Mul(a, a);
} return r;
} int main()
{
MT a;
while(~scanf("%d %d", &k, &mod))
{
for(int i = ; i < ; i++)
{
for(int j = ; j < ; j++)
{
if(j == i + ) a.m[i][j] = ;
else a.m[i][j] = ;
}
} for(int i = ; i >= ; i--)
{
scanf("%d", &a.m[][i]);
} if(k <= )
{
printf("%d\n", k % mod);
continue;
} int ans = ;
MT b = Product(a, k-); for(int i = ; i < ; i++)
{
ans += b.m[][i]*i % mod;
} printf("%d\n", ans % mod);
}
return ;
}

HDU1757的更多相关文章

  1. 矩阵快速幂(入门) 学习笔记hdu1005, hdu1575, hdu1757

    矩阵快速幂是基于普通的快速幂的一种扩展,如果不知道的快速幂的请参见http://www.cnblogs.com/Howe-Young/p/4097277.html.二进制这个东西太神奇了,好多优秀的算 ...

  2. hdu1757 A Simple Math Problem

    Problem Description Lele now is thinking about a simple function f(x).If x < 10 f(x) = x.If x > ...

  3. HDU1757 A Simple Math Problem 矩阵快速幂

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  4. hdu------(1757)A Simple Math Problem(简单矩阵快速幂)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  5. A Simple Math Problem HDU1757

    一次ac 在做递推关系的题目的时候  快速幂矩阵真的很有用 #include<iostream> #include<cstdio> #include<cstring> ...

  6. hdu1757 构造矩阵

    Lele now is thinking about a simple function f(x). If x < 10 f(x) = x.If x >= 10 f(x) = a0 * f ...

  7. 矩阵快速幂(以HDU1757为例)

    对于数据量大的求余运算,在有递推式的情况下,可以构造矩阵求解. A - A Simple Math Problem Lele now is thinking about a simple functi ...

  8. HDU1757:A Simple Math Problem(矩阵快速幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=1757 Problem Description Lele now is thinking about a simp ...

  9. HDU1757又是一道矩阵快速幂模板题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 按照题目的要求构造矩阵 //Author: xiaowuga //矩阵: //a0 a1 a2 ...

随机推荐

  1. springMVC入门案例

    1.配置文件的web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xs ...

  2. js中文逗号转英文逗号

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. cocos2d-x入门二 helloworld实例运行与创建

    本机环境:win7+VS2012+python2.7.8+cocos2d-x-3.8,另外本机已经配置android开发环境(java+eclipse+SDK+ADT),针对环境搭建后续会有一篇详细说 ...

  4. e.target和e.currentTarget区别

    直接上代码: body里: <div id="father"> father <div id="son"> son </div&g ...

  5. 【Python】更优的字符串格式化方式 -- "format"替代"%s"

    背景 前段时间看了一篇介绍Python的代码技巧的文章,建议格式化字符串时使用"format"代替使用"%",但是没有说明原因.各博客网站介绍相关用法的博客很多 ...

  6. angular2 自定义双向绑定属性

    import { Component, OnInit, Output, Input, EventEmitter } from '@angular/core'; @Component({ selecto ...

  7. Dubbo通过注解实现RPC调用

    启动Dubbo服务有2个方式,1是通过xml配置,2是通过注解来实现,这点和Spring相似. 采用XML配置如下:  <?xml version="1.0" encodin ...

  8. Kali之aircrack-ng

    本机装好设备及驱动 电脑本机装好Realtek RTL8187 Wireless驱动连接好USB无线驱动 把设备转接给虚拟机 win+R,启动VMware USB Arbitration Servic ...

  9. 设计模式--适配器模式C++实现

    适配器模式C++实现 1定义Adapter 将一个类的接口变成客户端所需要的另外一种借口,从而使远不因为接口不匹配而无法合作的两个雷能够一起工作 又叫变压器模式,包装模式Wrapper 2类图 角色分 ...

  10. Im4java+ImageMagick/GraphicsMagick

    im4java的jar包可以在maven库中找到. <dependency> <groupId>org.im4java</groupId> <artifact ...