HDU 1757 A Simple Math Problem(矩阵高速幂)
题目地址:HDU 1757
最终会构造矩阵了。事实上也不难,仅仅怪自己笨。。= =!
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|
然后依据矩阵的结合律,能够先把构造的矩阵的(k-9)次幂求出来。最后直接求第一个数。
代码例如以下:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm> using namespace std;
int mod, a[20];
struct matrix
{
int ma[20][20];
} init, res, s;
matrix Mult(matrix x, matrix y)
{
int i, j, k;
matrix tmp;
for(i=0; i<10; i++)
{
for(j=0; j<10; j++)
{
tmp.ma[i][j]=0;
for(k=0; k<10; k++)
{
tmp.ma[i][j]=(tmp.ma[i][j]+x.ma[i][k]*y.ma[k][j])%mod;
}
}
}
return tmp;
}
matrix Pow(matrix x, int k)
{
matrix tmp;
int i, j;
for(i=0; i<10; i++) for(j=0; j<10; j++) tmp.ma[i][j]=(i==j);
while(k)
{
if(k&1) tmp=Mult(tmp,x);
x=Mult(x,x);
k>>=1;
}
return tmp;
}
int main()
{
int k, x, i, j;
while(scanf("%d%d",&k,&mod)!=EOF)
{
if(k<10)
{
printf("%d\n",k%mod);
continue ;
}
for(i=9; i>=0; i--)
{
a[i]=9-i;
}
for(i=0; i<10; i++)
{
scanf("%d",&x);
init.ma[0][i]=x%mod;
}
for(i=1; i<10; i++)
{
for(j=0; j<10; j++)
{
init.ma[i][j]=(i==j+1);
}
}
res=Pow(init,k-9);
/*for(i=0; i<10; i++)
{
for(j=0; j<10; j++)
{
printf("%d ",res.ma[i][j]);
}
puts("");
}*/
int ans=0;
for(j=0; j<10; j++)
{
ans=(ans+res.ma[0][j]*a[j])%mod;
//printf("%d %d %d\n",res.ma[i][j],a[i],ans);
}
printf("%d\n",ans);
}
return 0;
}
HDU 1757 A Simple Math Problem(矩阵高速幂)的更多相关文章
- HDU 1757 A Simple Math Problem (矩阵快速幂)
题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...
- HDU 1757 A Simple Math Problem(矩阵)
A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...
- hdu 1757 A Simple Math Problem_矩阵快速幂
题意:略 简单的矩阵快速幂就行了 #include <iostream> #include <cstdio> #include <cstring> using na ...
- HDU 1757 A Simple Math Problem 【矩阵经典7 构造矩阵递推式】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1757 A Simple Math Problem Time Limit: 3000/1000 MS (J ...
- hdu 1757 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 ...
- 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(矩阵快速幂)
题目链接 题意 :给你m和k, 让你求f(k)%m.如果k<10,f(k) = k,否则 f(k) = a0 * f(k-1) + a1 * f(k-2) + a2 * f(k-3) + …… ...
- hdu 1757 A Simple Math Problem (矩阵快速幂)
Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x. If x >= 10 ...
随机推荐
- 解决cognos以远程db2数据库为数据源的连接失败问题
问题现象为使用远程的db2来创建数据源时,测试连接时不通,好多人都说是驱动问题,将db2cc.jar拷贝到某lib目录下,实验不通: 在看到某哥们的博客时最后提了一句,说需要将数据库catalog到本 ...
- 本人对于JavaScript的一些总结
类型.值和变量 1.原始类型 数字.字符串和布尔 null空 undefined未定义 2.对象类型 3.类 Array Function Date RegExp Error 4.js ...
- Android学习总结——实现Home键功能
实现Home键功能简而言之就是回到桌面,让Activity不销毁,程序后台运行. 实现方法: Intent intent= new Intent(Intent.ACTION_MAIN); intent ...
- Grails重定向以及Render、Session的使用
近期发现网上grails因为用的人不算非常多,相关的资料也不是那么easy找到,随着学习的深入打算把我的学习心得定期分享出来.也给学习grails的同胞一个偷懒的机会,我的原则是以最简洁的方式学会Gr ...
- Android应用程序请求SurfaceFlinger服务创建Surface的过程分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/7884628 前面我们已经学习过Android应 ...
- 【贪心+中位数】【UVa 11300】 分金币
(解方程建模+中位数求最短累积位移) 分金币(Spreading the Wealth, UVa 11300) 圆桌旁坐着n个人,每人有一定数量的金币,金币总数能被n整除.每个人可以给他左右相邻的人一 ...
- 面试前的准备---C#知识点回顾----01
过完年来,准备找份新工作,虽然手里的工作不错,但树挪死,人挪活.咱不能一直在一个坑里生活一辈子,外面的世界毕竟是很美好的. 为了能正常的找到自己中意的工作,最近是将所有的基础知识拿出来复习了一次.仅作 ...
- videojs 视频开发API
videojs就提供了这样一套解决方案,他是一个兼容html5的视频播放工具,早期版本兼容所有浏览器,方法是:提供三个后缀名的视频,并在不支持html5的浏览器下生成一个flash的版本. 最新的3. ...
- WebService应用一例,带有安全验证
1.创建WEB项目,添加WEB服务WebService1.asmx,代码如下: using System; using System.Collections.Generic; using System ...
- php单例设计模式
class car { static $obj = null; private function __construct(){} static function getObj(){ if(is_nul ...