hdu 1757 A Simple Math Problem (乘法矩阵)
A Simple Math Problem
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2441 Accepted Submission(s): 1415
If x < 10 f(x) = x.
If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);
And ai(0<=i<=9) can only be 0 or 1 .
Now, I will give a0 ~ a9 and two positive integers k and m ,and could you help Lele to caculate f(k)%m.
In each case, there will be two lines.
In the first line , there are two positive integers k and m. ( k<2*10^9 , m < 10^5 )
In the second line , there are ten integers represent a0 ~ a9.
开始没什么思路,后来还是没思路..
然后看解题报告,发现时乘法矩阵,关键点还是在构造矩阵上。
参考:http://blog.sina.com.cn/s/blog_79b832820100wnu3.html
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 =矩阵C
我们要求的是 f(k),就是矩阵C的最后一个元素,故依据矩阵的结合律,可看到
C=A*(A*(.....*(A*B))) ,要有k个A即 C=A^k*B ,然后就可以二分求A^k,最后乘上B就可以求得矩阵C
//0MS 232K 1214 B C++
#include<stdio.h>
#include<string.h>
#define N 15
struct matrix{
int g[N][N];
}ans,temp;
int g[N];
int m;
matrix mul(matrix a,matrix b)
{
matrix c;
for(int i=;i<;i++)
for(int j=;j<;j++){
c.g[i][j]=;
for(int k=;k<;k++)
c.g[i][j]+=a.g[i][k]*b.g[k][j];
c.g[i][j]%=m;
}
return c;
}
void solve(int k)
{
while(k){
if(k&) ans=mul(temp,ans);
temp=mul(temp,temp);
k/=;
}
int sum=;
/*
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
printf(j==9?"%d\n":"%d ",ans.g[i][j]);
*/
for(int i=;i<;i++){
sum+=ans.g[][i]*i;
sum%=m;
}
printf("%d\n",sum);
}
int main(void)
{
int k;
while(scanf("%d%d",&k,&m)!=EOF)
{
for(int i=;i<;i++)
for(int j=;j<;j++)
temp.g[i][j]=ans.g[i][j]=;
for(int i=;i<;i++)
scanf("%d",&g[i]);
for(int i=;i<;i++){
if(i<) temp.g[i][i+]=;
ans.g[i][i]=;
temp.g[][i]=g[-i];
}
solve(k-);
}
return ;
}
hdu 1757 A Simple Math Problem (乘法矩阵)的更多相关文章
- 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(矩阵高速幂)
题目地址:HDU 1757 最终会构造矩阵了.事实上也不难,仅仅怪自己笨..= =! f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 ...
- 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(矩阵快速幂乘法)
Problem Description Lele now is thinking about a simple function f(x). If x < f(x) = x. If x > ...
- 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 ...
- hdu 1757 A Simple Math Problem (矩阵快速幂,简单)
题目 也是和LightOJ 1096 和LightOJ 1065 差不多的简单题目. #include<stdio.h> #include<string.h> #include ...
- hdu 1757 A Simple Math Problem (构造矩阵解决递推式问题)
题意:有一个递推式f(x) 当 x < 10 f(x) = x.当 x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + ...
- hdu 1757 A Simple Math Problem (矩阵高速幂)
和这一题构造的矩阵的方法同样. 须要注意的是.题目中a0~a9 与矩阵相乘的顺序. #include <iostream> #include <cstdio> #include ...
随机推荐
- css transform skew变换
两个参数,x-保持纵坐标不变,所有点旋转逆时针旋转x度,y-横坐标不变所有点顺时针旋转y度
- Android Studio debug使用release的签名
当我们在做微信微博sdk分享的时候调试非常麻烦,因为要使用对应的签名版本才能调用sdk成功. 当我们使用AndroidStudio的Gradle之后会很简单的解决这个问题. 1.我们把签名文件放到工程 ...
- docker 命令汇总
命令汇总 docker history fa5fa5为镜像id或者镜像名 docker export 30b >h.tar30b为容器id或者容器名# docker export angry_b ...
- SocketTcpServer
自定义SocketTcpServer,虽然现在有很多这样的组件,但是有时候还是需要把它集成在你的框架或者产品中,不需要特别强大的功能,根据需求定制.最基本的一个问题是判断数据包的结束,没有像super ...
- winform中dataGridView高度自适应填充完数据的高度
// winform中dataGridView高度自适应填充完数据的高度,就是dataGridView自身不产生滚动条,自己的高度是根据数据的多少而变动. 在load的时候,数据绑定后,加上如下代码: ...
- PLSQL 逻辑多线程机制
CREATE OR REPLACE PACKAGE jv_para_pkg IS PROCEDURE main(i_job_name VARCHAR2, i_job ...
- ubuntu16.04解决播放swf视频文件问题
使用下面 sudo apt-get install swfdec-gnome
- 一、Owin Identity的使用
参照http://www.cnblogs.com/r01cn/p/5177708.html教程. 注意点: 1.Nuget包分别下载Microsoft.AspNet.Identity(Owin必须), ...
- centos6.4安装VMwareTools
centos6.4安装VMware Tools,同样适用于VMware.ESXi.Hypervisor #如果文件不存在需要创建 mkdir /mnt/cdrom #挂载光驱 mount -t aut ...
- AndroidManifest.xml
一.关于AndroidManifest.xml AndroidManifest.xml 是每个android程序中必须的文件.它位于整个项目的根目录,描述了package中暴露的组件(activiti ...