A Simple Math Problem

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4307    Accepted Submission(s): 2586

Problem Description
Lele now is thinking about a simple function f(x).

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.

 
Input
The problem contains mutiple test cases.Please process to the end of file.
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.
 
Output
For each case, output f(k) % m in one line.
 
Sample Input
10 9999
1 1 1 1 1 1 1 1 1 1
20 500
1 0 1 0 1 0 1 0 1 0
 
Sample Output
45
104
 
Author
linle
 
Source
 
题意:
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 .
上面的递推式计算f(k)。计算结果%m;
代码:
 //普通递推会超时,用矩阵乘法。构造矩阵时右上角的(n-1)*(n-1)矩阵置为单位矩阵,第n行从 右 到 左 填入系数,快速幂之后第n行作为行矩阵乘递推式从 小 到 大 排列的列矩阵得到结果
//或者构造左下角的单位矩阵,第一行从 左 到 右 填入系数,快速幂之后第1行作为行矩阵乘递推式从 大 到 小 排列的列矩阵得到结果
#include<bits\stdc++.h>
using namespace std;
int a[],f[]={,,,,,,,,,};
int k,m;
struct Lu
{
int mp[][];
}L1;
void init()
{
memset(L1.mp,,sizeof(L1.mp));
for(int i=;i<=;i++)
{
L1.mp[i][i+]=;
}
for(int i=;i<=;i++)
{
L1.mp[][i]=a[-i];
}
}
Lu mult(Lu a,Lu b)
{
Lu c;
for(int i=;i<=;i++)
for(int j=;j<=;j++)
{
c.mp[i][j]=;
for(int k=;k<=;k++)
c.mp[i][j]+=(a.mp[i][k]*b.mp[k][j])%m;
c.mp[i][j]%=m;
}
return c;
}
Lu solve(int x)
{
if(x==)
return L1;
if(x&)
{
Lu q=solve(x-);
return mult(q,L1);
}
else
{
Lu q=solve(x/);
return mult(q,q);
}
}
int main()
{
while(scanf("%d%d",&k,&m)!=EOF)
{
for(int i=;i<=;i++)
scanf("%d",&a[i]);
if(k<)
{
printf("%d\n",k%m);
continue;
}
init();
Lu tem=solve(k-);
int ans=;
for(int i=;i<=;i++)
ans+=(tem.mp[][i]*f[i])%m;
printf("%d\n",ans%m);
}
return ;
}
 //普通递推会超时,用矩阵乘法。
#include<bits\stdc++.h>
using namespace std;
int a[],f[]={,,,,,,,,,};
int k,m;
struct Lu
{
int mp[][];
}L1;
void init()
{
memset(L1.mp,,sizeof(L1.mp));
for(int i=;i<=;i++)
{
L1.mp[i][i-]=;
}
for(int i=;i<=;i++)
{
L1.mp[][i]=a[i];
}
}
Lu mult(Lu a,Lu b)
{
Lu c;
for(int i=;i<=;i++)
for(int j=;j<=;j++)
{
c.mp[i][j]=;
for(int k=;k<=;k++)
c.mp[i][j]+=(a.mp[i][k]*b.mp[k][j])%m;
c.mp[i][j]%=m;
}
return c;
}
Lu solve(int x)
{
if(x==)
return L1;
if(x&)
{
Lu q=solve(x-);
return mult(q,L1);
}
else
{
Lu q=solve(x/);
return mult(q,q);
}
}
int main()
{
while(scanf("%d%d",&k,&m)!=EOF)
{
for(int i=;i<=;i++)
scanf("%d",&a[i]);
if(k<)
{
printf("%d\n",k%m);
continue;
}
init();
Lu tem=solve(k-);
int ans=;
for(int i=;i<=;i++)
ans+=(tem.mp[][i]*f[i])%m;
printf("%d\n",ans%m);
}
return ;
}

*HDU 1757 矩阵乘法的更多相关文章

  1. hdu 1757 矩阵

    用矩阵表示状态,矩阵乘法的就是状态之间的变换 作一个vector: 要求的就是一个矩阵A,使得上面那个vector乘以A之后变成 解得A= [不知道用逆矩阵能不能直接求出A Ref:http://bl ...

  2. hdu 1757 (矩阵快速幂) 一个简单的问题 一个简单的开始

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题意不难理解,当x小于10的时候,数列f(x)=x,当x大于等于10的时候f(x) = a0 * ...

  3. hdu 1757 矩阵快速幂 **

    一看正确率这么高,以为是水题可以爽一发,结果是没怎么用过的矩阵快速幂,233 题解链接:点我 #include<iostream> #include<cstring> ; us ...

  4. HDU 1757 矩阵快速幂加速递推

    题意: 已知: 当x<10时:f(x)=x 否则:f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + --+ a9 * f(x-10); 求:f(x ...

  5. HDU 1757 矩阵求第n的递推式

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  6. hdu 1757 矩阵连乘

  7. Hdu 4920矩阵乘法(内存访问的讲究)

    题目链接 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K ( ...

  8. hdu 3483 矩阵乘法

    这个题目上周对抗赛题目,搞了我好久 对数学这种不是很敏感 其实都不是自己想出来的,看其他的资料和博客的推导 还是有点难度的,反正我是推不出来 通过二项式定理的化简 有两个博客写得比较好 http:// ...

  9. 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 ...

随机推荐

  1. 【python】发送post请求

    1. json格式的post请求 关键部分加粗显示了,主要是post数据的编码方式以及请求头的Content-type #coding=utf8 import json import gzip imp ...

  2. Servlet引擎Jetty之入门1

    Jetty与tomcat一样,HttpWeb容器,支持实现Servlet规范. 详细介绍参考:https://www.ibm.com/developerworks/cn/java/j-lo-jetty ...

  3. 解决css3毛玻璃效果(blur)有白边问题

    做一个登录页,全屏背景图毛玻璃效果,实现方法如下: HTML: <body> <div class="login-wrap"> <div class= ...

  4. 怎么使用Delphi获取当前的时间,精确到毫秒

    先介绍一个可能比较常用的方法,获取当前时间 var datetime: string; begin datetime:= FormatDateTime('yyyy-mm-dd hh:mm:ss', N ...

  5. Linux学习内容

    Linux学习要点(转载自红联) 一.学习Linux的基本要求1. 掌握至少50个以上的常用命令. 2. 熟悉Gnome/KDE等X-windows桌面环境操作 . 3. 掌握.tgz..rpm等软件 ...

  6. 移动端webapp自适应实践(css雪碧图制作小工具实践)图文并茂

    为什么要写这个 以前写过关于webapp自适应屏幕的文章(链接),不过写的大多数群众看不懂,所以来个图文并茂的版本.虽然只是一个简单的页面,不过在做的过程中也遇到了一些问题,也算是好事吧! 该示例gi ...

  7. C# 图片无损压缩

    /// <summary> /// 图像缩略图处理 /// </summary> /// <param name="bytes">图像源数据&l ...

  8. LinkedHashMap源码阅读笔记(基于jdk1.8)

    LinkedHashMap是HashMap的子类,很多地方都是直接引用HashMap中的方法,所以需要注意的地方并不多.关键的点就是几个重写的方法: 1.Entry是继承与Node类,也就是Linke ...

  9. 会议通js

    js逻辑: /** * Created by wanglijuan on 2016/12/2. */ $(function () { //登陆后请求数据 // $.ajax({ // url:&quo ...

  10. SVN

    你先在本地把包删了,然后提交上级目录就可以了.比如你要删除com.aaa.A这个类.你就把A删了,然后提交aaa就行,如果你要把aaa包删了,你就删了以后提交com这个包.SVN就删掉了 svn提交更 ...