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

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
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  1588 3117 2276 2256 2254 
 

开始没什么思路,后来还是没思路..

然后看解题报告,发现时乘法矩阵,关键点还是在构造矩阵上。

参考: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 (乘法矩阵)的更多相关文章

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

  2. HDU 1757 A Simple Math Problem (矩阵乘法)

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

  3. HDU 1757 A Simple Math Problem(矩阵高速幂)

    题目地址:HDU 1757 最终会构造矩阵了.事实上也不难,仅仅怪自己笨..= =! f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 ...

  4. 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) + …… ...

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

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

  7. hdu 1757 A Simple Math Problem (矩阵快速幂,简单)

    题目 也是和LightOJ 1096 和LightOJ 1065 差不多的简单题目. #include<stdio.h> #include<string.h> #include ...

  8. 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) + ...

  9. hdu 1757 A Simple Math Problem (矩阵高速幂)

    和这一题构造的矩阵的方法同样. 须要注意的是.题目中a0~a9 与矩阵相乘的顺序. #include <iostream> #include <cstdio> #include ...

随机推荐

  1. python3_phantomJS_test

    phantomJS和selenium差不多,几乎不相上下,使用会麻烦一点,但是比selenium快很多: # !/usr/bin/python3.4 # -*- coding: utf-8 -*- f ...

  2. mybatis 使用记录

    首先,贴一个好文.把mybatis中的<if></if>的条件扒到源码分析了一遍.PS:目前还看不懂... http://cheng-xinwei.iteye.com/blog ...

  3. Spring-RMI固定端口

    Spring-RMI固定端口 最近接到一个需求项目所应用的RMI端口(数据传输端口)为随机指定的,项目要求对其端口固定,费劲周折找了很多资料,最后解决了问题.   其实解决问题的方法及其简单,只需要在 ...

  4. Hbase客户端API基础小结笔记(未完)

    客户端API:基础 HBase的主要客户端接口是由org.apache.hadoop.hbase.client包中的HTable类提供的,通过这个类,用户可以完成向HBase存储和检索数据,以及删除无 ...

  5. linux 文档处理命令

    1. 将用户信息数据库文件和组信息数据纵向合并为一个文件/1.txt(覆盖) 2.将用户信息数据库文件和用户密码数据库文件纵向合并为一个文件/2.txt(追加 3.将/1.txt./2.txt两个文件 ...

  6. 使用 Sublime、WebStorm 开发 Jade

    Sublime.WebStorm (PhpStorm) 是前端开发者的得力工具,开发 Jade 也不例外. 在配置这些软件的 Jade 开发环境前,请先在系统中安装 Node.js 和 Jade : ...

  7. 每天一个Linux命令(3):pwd命令

    Linux中用 pwd 命令来查看"当前工作目录"的完整路径. 简单得说,每当你在终端进行操作时,你都会有一个当前工作目录. 在不太确定当前位置时,就会使用pwd来判定当前目录在文 ...

  8. css学习笔记(4)

    让顶部导航固定于页面的最顶端,无论页面上下滚动,顶部导航始终处在最顶端. *{ margin:0; padding:0}body{ padding-top:60px; }#nav{ width:100 ...

  9. 技术那么多,你想看看JSON Schema的测试吗?

    目录 1. 什么是JSON Schema? 2. 如何定义一个JSON Schema 3. 如何测试JSON Schema a) 使用JSON Schema validator GUI b) 在Jav ...

  10. js分辨浏览器类别和版本

    function BrowserInfo() { var ua = navigator.userAgent.toLowerCase(); var Sys = {}; var s; (s = ua.ma ...