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 这就算我写的第一道矩阵快速幂的题了!
矩阵快速幂的给我的感觉就是先找规律,YY出一个n*n的矩阵,矩阵左边写一列数,矩阵右边写一列数。让矩阵乘上右边那一列数等于左边的那列数。盗张图!!

然后......然后就没有然后了QWQ,输出答案就行了。
代码如下:
 #include <bits/stdc++.h>

 using namespace std;
const int N=;
int k,m;
struct Matrix//用结构体来存矩阵
{
long long int mat[N][N];//直接上long long int 别找事
}matrix;
void init()
{
for (int i=;i<N;++i)
scanf("%lld",&matrix.mat[][i]);
for (int i=;i<N;++i)
{
for (int j=;j<N;++j)
{
if (i==j+)
matrix.mat[i][j]=;
else
matrix.mat[i][j]=;
}
}
}
Matrix operator * (Matrix a,Matrix b)//定义乘号
{
Matrix c;
for (int i=;i<N;++i)
{
for (int j=;j<N;++j)
{
c.mat[i][j]=;//初始化值为0
for (int k=;k<N;++k)
c.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
c.mat[i][j]%=m;//记得取模,否则就炸了
}
}
return c;
}
Matrix Pow (int n)//定义快速幂函数
{
Matrix t;
if (n==)
return matrix;
if (n&)
return matrix*Pow(n-);
else
{
Matrix temp=Pow(n>>);
return temp*temp;
}
}
int main()
{
//freopen("de.txt","r",stdin);
while (~scanf("%d%d",&k,&m))
{
init();
if (k<)
{
printf("%lld\n",matrix.mat[][k]%m);
continue;
}
Matrix temp=Pow(k-);
long long int ans=;
for (int i=;i<N;++i)
{
ans += temp.mat[][i]*(N-i-);
ans%=m;
}
printf("%lld\n",ans);
}
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 (矩阵快速幂,简单)

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

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

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

随机推荐

  1. mybatis框架之动态代理

    坦白讲,动态代理在日常工作中真没怎么用过,也少见别人用过,网上见过不少示例,但总觉与装饰模式差别不大,都是对功能的增强,什么前置后置,其实也就那么回事,至于面试中经常被问的mybatis框架mappe ...

  2. loadrunner(预测系统行为和性能的负载测试工具)

    LoadRunner,是一种预测系统行为和性能的负载测试工具.通过以模拟上千万用户实施并发负载及实时性能监测的方式来确认和查找问题,LoadRunner能够对整个企业架构进行测试.企业使用LoadRu ...

  3. winserver安装redis

    原文: https://www.cnblogs.com/xuzhiwei/p/4569315.html 1.Redis本身不支持windows,但是有另外的团队在维护着一个windows下的版本   ...

  4. [CSP-S模拟测试]:chinese(数学)

    题目传送门(内部题25) 输入格式 一行三个整数$n,m,k$. 输出格式 一行一个整数表示答案. 样例 样例输入: 2 2 2 样例输出: 数据范围与提示 样例解释: $f_0=10,f_1=4,f ...

  5. 阿里云使用ssl免费证书

    购买免费证书 购买之后 申请证书 该域名必须添加一条TXT记录 根据提示添加记录 下载证书 我用的nginx做的映射,所以下载nginx nginx安装自行百度 将下载的文件解压到nginx目录下(创 ...

  6. js 判断对象的长度

    Object.size = function(obj) { var size = 0, key; for (key in obj) { if (obj.hasOwnProperty(key)) siz ...

  7. js判断客户端是pc还是移动端

    navigator.userAgentNavigator 对象包含有关浏览器的信息.没有应用于 navigator 对象的公开标准,不过所有浏览器都支持该对象.userAgent 属性是一个只读的字符 ...

  8. 每天一个linux常用命令--ls 和 -ll 有什么区别?

    一.-ls 和 -ll  有什么区别? 1. ls 命令可以说是linux下最常用的命令之一.ll不是命令,是ls -l的别名相当于windows里的快捷方式.所以"ll"和“ls ...

  9. css 样式小窍门

    css 样式小窍门 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  10. 《单词的减法》state1~state17(200p)

    单词的减法 2016.05.18 state 1 absent accessible accordingly accuracy/accurate acquaint/acquaintance adequ ...