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

If x <  f(x) = x.
If x >= f(x) = a0 * f(x-) + a1 * f(x-) + a2 * f(x-) + …… + a9 * f(x-);
And ai(<=i<=) can only be or . 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<*^ , m < ^ )
In the second line , there are ten integers represent a0 ~ a9.
Output
For each case, output f(k) % m in one line.
 
Sample Input

 
Sample Output

把问题转化为求矩阵的n-9次幂就行了;

 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int k,MOD;
int a[];
int f[];
struct Matrix
{
int m[][];
}matrix; Matrix Mul(Matrix a,Matrix b)
{
Matrix res;
int i,j,k;
for(i=;i<;i++)
{
for(j=;j<;j++)
{
res.m[i][j] = ;
for(k=;k<;k++)
res.m[i][j] = (res.m[i][j]+(a.m[i][k]*b.m[k][j]))%MOD;
}
}
return res;
} Matrix fastm(Matrix a,int b)
{
Matrix res;
memset(res.m,,sizeof(res.m));
for(int i=;i<;i++)
res.m[i][i] = ;
while(b)
{
if(b&)
res = Mul(res,a);
a = Mul(a,a);
b >>= ;
}
return res;
}
void init()
{
for(int i=;i<=;i++)
{
f[i]=i;
}
}
int main()
{
init();
while(scanf("%d%d",&k,&MOD)==)
{
for(int i=;i<=;i++)
{
scanf("%d",&a[i]);
}
if(k<)
{
printf("%d\n",k%MOD);
continue;
} memset(matrix.m,,sizeof(matrix.m));
for(int i=;i<=;i++)
matrix.m[][i]=a[i];
for(int i=;i<=;i++)
matrix.m[i][i-] = ;
Matrix ans=fastm(matrix,k-);
Matrix cnt;
for(int i=;i<;i++)
{
cnt.m[i][]=f[-i];
}
Matrix p=Mul(ans,cnt);
printf("%d\n",p.m[][]%MOD);
}
return ;
}

hdu 1757 A Simple Math Problem(矩阵快速幂乘法)的更多相关文章

  1. HDU 1757 A Simple Math Problem (矩阵快速幂)

    题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...

  2. hdu 1757 A Simple Math Problem_矩阵快速幂

    题意:略 简单的矩阵快速幂就行了 #include <iostream> #include <cstdio> #include <cstring> using na ...

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

    A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...

  4. HDU1757 A Simple Math Problem 矩阵快速幂

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

  5. A Simple Math Problem(矩阵快速幂)----------------------蓝桥备战系列

    Lele now is thinking about a simple function f(x).  If x < 10 f(x) = x.  If x >= 10 f(x) = a0 ...

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

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

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

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

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

  10. HDU 1757 A Simple Math Problem( 矩阵快速幂 )

    <font color = red , size = '4'>下列图表转载自 efreet 链接:传送门 题意:给出递推关系,求 f(k) % m 的值, 思路: 因为 k<2 * ...

随机推荐

  1. 蓝桥杯 六角形中填置1~12个数字 dfs

    如图[1.png]所示六角形中,填入1~12的数字. 使得每条直线上的数字之和都相同. 图中,已经替你填好了3个数字,请你计算星号位置所代表的数字是多少? 请通过浏览器提交答案,不要填写多余的内容. ...

  2. HDU1232 畅通工程 (并查集模板题)

    畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  3. ubuntu下集群设置静态ip

    hadoop集群时,需要固定集群内计算机相互通信之间的ip地址,但是每次进行网络连接后,ip地址都是变换的,我们希望设置一个用于集群内通信的静态ip,即使重启电脑也不会变化,同样希望能够正常的访问互联 ...

  4. IOS 添加到通讯录

    addressBookHelper.h #import <Foundation/Foundation.h> enum { ABHelperCanNotConncetToAddressBoo ...

  5. iOS SDK原生JSON解析

    - (IBAction)touchReadButton:(id)sender { NSData *jsonData = [[NSData alloc] initWithContentsOfFile:J ...

  6. .Net语言中关于AOP 的实现详解

    来源: IT人家  发布时间: 2011-03-22 20:28  阅读: 3546 次  推荐: 2   原文链接   [收藏] 摘要:该文章主要和大家讲解开发应用系统时在.Net语言中关于AOP ...

  7. struts2 action重定向

    struts2的结果类型: <action name="loginAction" class="com.itheima.action.LoginAction&quo ...

  8. 单击事件的处理方式及注册窗体的创建之(四)Intent实现界面跳转传值

    跳转开发步骤: 创建Intent对象  设置Intent对象的跳转路径  启动Intent //Register_Activity.java case R.id.btnRegister: Inte ...

  9. Java的动态代理机制详解(转)

    在学习Spring的时候,我们知道Spring主要有两大思想,一个是IoC,另一个就是AOP,对于IoC,依赖注入就不用多说了,而对于Spring的核心AOP来说,我们不但要知道怎么通过AOP来满足的 ...

  10. Marineking wilyin

    A - Marineking wilyin Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Ot ...