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

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
#include<cmath>
const int maxn=1e5+5;
typedef long long ll;
using namespace std; ll mod;
ll op[15];
struct mat
{
ll a[15][15];
};
mat Mul(mat a,mat b)
{
mat ans;
memset(ans.a,0,sizeof(ans.a));
for(int t=0;t<10;t++)
{
for(int j=0;j<10;j++)
{
for(int k=0;k<10;k++)
{
ans.a[t][j]=(ans.a[t][j]+a.a[t][k]*b.a[k][j])%mod;
}
}
}
return ans;
}
mat ans; ll quick(ll n)
{
mat res;
memset(res.a,0,sizeof(res.a));
for(int t=0;t<10;t++)
{
res.a[0][t]=op[t];
}
res.a[1][0]=1;
res.a[2][1]=1;
res.a[3][2]=1;
res.a[4][3]=1;
res.a[5][4]=1;
res.a[6][5]=1;
res.a[7][6]=1;
res.a[8][7]=1;
res.a[9][8]=1;
while(n)
{
if(n&1)
{
ans=Mul(res,ans);
}
res=Mul(res,res);
n>>=1;
}
return ans.a[0][0];
}
int main()
{
ll n;
while(cin>>n)
{
cin>>mod;
for(int t=0;t<10;t++)
{
scanf("%lld",&op[t]);
}
if(n<10)
{
printf("%lld\n",n%mod);
}
else{ for(int t=9;t>=0;t--)
{
ans.a[9-t][0]=t;
}
printf("%lld\n",quick(n-9)%mod);
}
} return 0;
}

A Simple Math Problem(矩阵快速幂)----------------------蓝桥备战系列的更多相关文章

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

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

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

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

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

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

  4. BestCoder Round #29——A--GTY's math problem(快速幂(对数法))、B--GTY's birthday gift(矩阵快速幂)

    GTY's math problem Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

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

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

  6. hdu-1757 A Simple Math Problem---矩阵快速幂模板题

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1757 题目大意: 求递推式第k项模m If x < 10 f(x) = x.If x > ...

  7. A Simple Math Problem 矩阵打水题

    A Simple Math Problem Lele now is thinking about a simple function f(x).If x < 10 f(x) = x.If x & ...

  8. HDU - 3521 An easy Problem(矩阵快速幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=3521 题意 对于矩阵A,求e^A的值. 分析 这个定眼一看好像很熟悉,就是泰勒展开,可惜自己的高数已经还给老师了 ...

  9. hdu 6182A Math Problem(快速幂)

    You are given a positive integer n, please count how many positive integers k satisfy kk≤nkk≤n.  Inp ...

随机推荐

  1. 修改Tomcat可支持get传参方式的url长度,get形式

    maxHttpHeaderSize="8192"加在 <Connector port="8081" maxHttpHeaderSize="314 ...

  2. Java虚拟机学习总结之OutOfMemoryError异常

    参考:深入理解java虚拟机一书 开始之前,我们也应当搞清楚连个概念,内存泄漏Memory Leak 内存溢出: 内存泄漏:程序中间动态分配了内存,但是在程序结束时没有释放内存,造成这部分内存不可用. ...

  3. 39.FORMAT() 函数

    FORMAT() 函数 FORMAT 函数用于对字段的显示进行格式化. SQL FORMAT() 语法 SELECT FORMAT(column_name,format) FROM table_nam ...

  4. 打印单据,A4纸,每个单据占一个A4纸,两个单据之间不挨着

    打印单据,A4纸,每个单据占一个A4纸,两个单据之间不挨着 <style type="text/css" media="print">.Noprin ...

  5. Entity Framework 6.0 Tutorials(3):Code-based Configuration

    Code-based Configuration: Entity Framework 6 has introduced code based configuration. Now, you can c ...

  6. servlet模板

    package ${enclosing_package};import java.io.IOException;import javax.servlet.ServletException;import ...

  7. Linq学习<二>

    http://www.cnblogs.com/wyqlijin/archive/2011/02/25/1964934.html 这位仁兄写的比较高深,建议大家看看 一: 这一篇以一个数据类为例,操作数 ...

  8. Java 线程不安全问题分析

    当多个线程并发访问同一个资源对象时,可能会出现线程不安全的问题 public class Method implements Runnable { private static int num=50; ...

  9. vi常用快捷键汇总

      第一部分:光标移动.复制粘贴.查找替换 [Ctrl] + [f]  下一页 [Ctrl] + [b]  上一页  0或[home]  到行首 $或[end]  到行尾  G  到最后一行  gg ...

  10. C#帮助类:Base64

    public class Base64 { #region Base64加密 ///<summary> ///Base64加密 ///</summary> ///<par ...