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 ...
随机推荐
- Basic Authentication in ASP.NET Web API
Basic authentication is defined in RFC 2617, HTTP Authentication: Basic and Digest Access Authentica ...
- LeetCode——Edit Distance
Question Given two words word1 and word2, find the minimum number of steps required to convert word1 ...
- Graph_Master(连通分量_C_Trajan缩点+最小路径覆盖)
hdu_3861 题目大意:给定一张有向图,若<u,v>可达(u可以走到v,或者 v可以走到u),则<u,v>需被划分在统一城邦,问最小划分城邦数. 题解:比较裸的题,可以看出 ...
- MCM写作
MCM 写作 引言的写作 对赛题的解读. 对现有研究成果的松树与评论 对解题思路和主要方法进行简介 引言第一句话是最重要的,应该激发读者阅读兴趣,应该浅显易懂,不用或者少用数学公式 赛题选定以后要首先 ...
- Zookeeper Zkclient客户端
Zkclient是对Zookeeper的原生API进行了包装,实现了超时重连.Watcher反复注册等功能,它可以实现递归创建,删除节点,但是zkClient不能递归给节点赋值. 主要的api如下: ...
- javaScript tips —— z-index 对事件机制的影响
demo // DOM结构 class App extends React.Component { componentDidMount() { const div1 = document.getEle ...
- Android----- MD5加密(登录注册得到与IOS相同的加密值,并且解决两个平台汉字加密不相同问题)
最近开发项目中遇到一个这样的问题,注册和登录时需要对信息MD5加密生成一个Token传给后台, 后台会对信息进行比较加密是否相同,才表示你登录或者注册成功,所以,IOS和Android两个平台的tok ...
- 1-22-shell脚本基本应用-实验手册
脚本应用思路 1. 确定命令操作(设计并执行任务) 2. 编写Shell脚本(组织任务过程) 3. 设置计划任务(控制时间,调用任务脚本) ------------------------------ ...
- js判断回车,判断焦点控件
document.onkeydown=function(event){ e = event ? event :(window.event ? window.event : null); ...
- C#/JAVA 程序员转GO/GOLANG程序员笔记大全(DAY 02)
------------------- 指针 go 保留的 c 语言指针的操作,同时增加了自动垃圾回收机制 var a = new(int) *a = // &a 内存地址 --------- ...