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 ...
随机推荐
- CF 39E What Has Dirichlet Got to Do with That? (博弈)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 题意:给出a ^ b,两个人轮流操作,可以 a ...
- java转换字符串编码格式 (解码错误,重新解码)
字符集概念:规定了某个文字对应的二进制数字存放方式(编码)和某串二进制数值代表了哪个文字(解码)的转换关系. 我们在计算机屏幕上看到的是实体化的文字,而在计算机存储介质中存放的实际是二进制的比特流. ...
- Java程序员面试题集(1-50)(转)
转:http://blog.csdn.net/jackfrued/article/details/17339393 下面的内容是对网上原有的Java面试题集及答案进行了全面修订之后给出的负责任的题目和 ...
- Hacker(五)----黑客专用通道--->端口
计算机中,端口是计算机与外部进行通信交流的出口.计算机本身携带的物理端口(键盘.鼠标.显示器等输入/输出接口)已经无法满足网络通信的要求,因此TCP/IP协议就引入了一种称为Socket的应用程序接口 ...
- Oracle 10G 使用UTL_SMTP发送中文电子邮件[Z]
CREATE OR REPLACE PROCEDURE SCOTT.HTML_EMAIL( P_TO IN VARCHAR2, --收件人地址 P_SUBJECT IN VARCHAR2, --邮件主 ...
- WdatePicker日历控件使用方法(转)
转自:http://www.cnblogs.com/weixing/archive/2011/08/15/2139431.html WdatePicker日历控件使用方法 1. 跨无限级框架显示 ...
- GoEasy实现web实时推送过程中的自动补发功能
熟悉GoEasy推送的朋友都知道GoEasy推送实现web实时推送并且能够非常准确稳定地将信息推送到客户端.在后台功能中查看接收信息详情时,可有谁注意到有时候在发送记录里有一个红色的R标志?R又代表的 ...
- Spring-----7、bean实例的创建方式及依赖配置
转载自:http://blog.csdn.net/hekewangzi/article/details/45648579
- Bower安装
一.安装Node.js: 1.下载Node.js并安装: http://nodejs.org/ 2.双击安装,默认C盘:C:\Program Files\nodejs 3.cmd进入上述目录: ...
- SlimDX的DirectSound模块
网上SlimDX的资源很少,搜到了http://www.xukailun.me/article/238/这篇关于<SlimDX的DirectSound模块应用实战>的文章,备份下来以备不时 ...