HDU1757
解题思路:分析需要不少时间,比较懒,直接把别人的分析贴在这里,
然后贴上自己写的代码:
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的更多相关文章
- 矩阵快速幂(入门) 学习笔记hdu1005, hdu1575, hdu1757
矩阵快速幂是基于普通的快速幂的一种扩展,如果不知道的快速幂的请参见http://www.cnblogs.com/Howe-Young/p/4097277.html.二进制这个东西太神奇了,好多优秀的算 ...
- 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 > ...
- HDU1757 A Simple Math Problem 矩阵快速幂
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- hdu------(1757)A Simple Math Problem(简单矩阵快速幂)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- A Simple Math Problem HDU1757
一次ac 在做递推关系的题目的时候 快速幂矩阵真的很有用 #include<iostream> #include<cstdio> #include<cstring> ...
- hdu1757 构造矩阵
Lele now is thinking about a simple function f(x). If x < 10 f(x) = x.If x >= 10 f(x) = a0 * f ...
- 矩阵快速幂(以HDU1757为例)
对于数据量大的求余运算,在有递推式的情况下,可以构造矩阵求解. A - A Simple Math Problem Lele now is thinking about a simple functi ...
- HDU1757:A Simple Math Problem(矩阵快速幂)
http://acm.hdu.edu.cn/showproblem.php?pid=1757 Problem Description Lele now is thinking about a simp ...
- HDU1757又是一道矩阵快速幂模板题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 按照题目的要求构造矩阵 //Author: xiaowuga //矩阵: //a0 a1 a2 ...
随机推荐
- list<>泛型的意义
泛型就是指定一个自定类或数据类型例如(int)并命名一个XXX集合名,所有这个类型的数据可以加入这个XXXX集合名,组成一个集合. private list<可放例int数据类型或自定类> ...
- 【医学图像】3D Deep Leaky Noisy-or Network 论文阅读(转)
文章来源:https://blog.csdn.net/u013058162/article/details/80470426 3D Deep Leaky Noisy-or Network 论文阅读 原 ...
- Python学习札记(四) Basic-1
参考:Python基础 Basic 1.以#开头的是注释. 2.解释器把每一行都当做是一个语句,当语句以冒号:结尾时,缩进的语句视为代码块. 3.请使用4个空格作为缩进,慎用Tab(请把Tab设置为4 ...
- Object.defineProperties——MEAN开发后台的Model层
Object.defineProperties是什么?有什么用? 这个问题比较听起来可能比较难以理解,确实我也是在项目中遇到的才会去想.以前看到<高级程序设计>的时候,有这么一种东西,定义 ...
- [小问题笔记(二)] 可能导致DropDownList的SelectedIndexChanged事件不触发的几种情况
遇到SelectedIndexChanged事件没有触发,可以依次检查以下几种情况是否在程序中出现. 一.DropDownList的不同option设置了相同的value . 二.没有写 AutoPo ...
- 今天 学习用到的一些知识(properties 读取,js 删除元素)
1.properties文件位置的关系:当properties文件放在src目录下时,编译会自动把src里的文件放到bin文件平级,因此可用this.getClass.getClassLoader.g ...
- Linux安装keepalived
1.下载安装ipvs安装包,进行解压 http://www.keepalived.org/software/ 2.创建安装路径连接 安装环境: yum -y install openssl-devel ...
- 重新学习MySQL数据库8:MySQL的事务隔离级别实战
重新学习Mysql数据库8:MySQL的事务隔离级别实战 在Mysql中,事务主要有四种隔离级别,今天我们主要是通过示例来比较下,四种隔离级别实际在应用中,会出现什么样的对应现象. Read unco ...
- Qt5.3中qml ApplicationWindow设置窗口无边框问题
这个版本的qt在这里有点bug.. 设置ApplicationWindow的flags属性为Qt.FramelessWindowHint的确可以使程序无边框,但是同时程序在任务栏的图标也没了. 看文档 ...
- CentOS上部署Django+Nginx+Uwsgi环境
在CentOS上部署Django+Nginx+Uwsgi环境 奇谭 2016-09-01 评论 Linux python django nginx uwsgi VirtualEnv的作用:创建隔 ...