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 * 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
 
 
 
 
 1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <cmath>
5 #include <algorithm>
6 #include <string>
7 #include <vector>
8 #include <set>
9 #include <map>
10 #include <stack>
11 #include <queue>
12 #include <sstream>
13 #include <iomanip>
14 using namespace std;
15 typedef long long LL;
16 const int INF = 0x4fffffff;
17 const double EXP = 1e-5;
18 const int MS = 1005;
19 const int SIZE = 10;
20
21 typedef vector<vector<int> > mat;
22
23 mat matrix(SIZE,vector<int>(SIZE));
24
25 int n,mod;
26
27 mat mul(mat &m1,mat &m2)
28 {
29 mat m(SIZE,vector<int>(SIZE));
30 for(int i=0;i<SIZE;i++)
31 for(int j=0;j<SIZE;j++)
32 for(int k=0;k<SIZE;k++)
33 {
34 m[i][j]=(m1[i][k]*m2[k][j]+m[i][j])%mod;
35 }
36 return m;
37 }
38
39 mat pow(int n)
40 {
41 mat m(SIZE,vector<int>(SIZE));
42 for(int i=0;i<SIZE;i++)
43 m[i][i]=1;
44 while(n)
45 {
46 if(n&1)
47 m=mul(m,matrix);
48 matrix=mul(matrix,matrix);
49 n>>=1;
50 }
51 return m;
52 }
53
54 int main()
55 {
56 while(scanf("%d%d",&n,&mod)!=EOF)
57 {
58 for(int i=0;i<SIZE;i++)
59 for(int j=0;j<SIZE;j++)
60 matrix[i][j]=(i-1)==j;
61 for(int i=0;i<SIZE;i++)
62 scanf("%d",&matrix[0][i]);
63
64 if(n<10)
65 {
66 printf("%d\n",n%mod);
67 continue;
68 }
69 matrix=pow(n-9);
70 int ans=0;
71 for(int i=0;i<SIZE;i++)
72 ans+=matrix[0][i]*(9-i)%mod;
73 printf("%d\n",ans%mod);
74 }
75 }

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(矩阵)

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

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

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

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

  5. HDU1757-A Simple Math Problem,矩阵快速幂,构造矩阵水过

    A Simple Math Problem 一个矩阵快速幂水题,关键在于如何构造矩阵.做过一些很裸的矩阵快速幂,比如斐波那契的变形,这个题就类似那种构造.比赛的时候手残把矩阵相乘的一个j写成了i,调试 ...

  6. A Simple Math Problem(矩阵快速幂)(寒假闭关第一题,有点曲折啊)

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

  7. hdu 1757 A Simple Math Problem (乘法矩阵)

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

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

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

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

随机推荐

  1. cnetos 网卡绑定 eth0+eth1做双网卡绑定到bond0

    1.网卡绑定:eth0+eth1做双网卡绑定到bond0 二.网络配置 网卡绑定1./etc/sysconfig/network-scripts/目录下建立ifcfg-bond0文件,内容如下DEVI ...

  2. System Verilog MCDF(二)

    整形器的接口时序: reg,grant是维持了两个clk的. chid ,length在发送数据期间不可以变化. 第一个data数据必须在start上升沿的同一个clk发送. reg,grant两者之 ...

  3. mybatis-plus还可以这样分表

    为什么要分表 Mysql是当前互联网系统中使用非常广泛的关系数据库,具有ACID的特性. 但是mysql的单表性能会受到表中数据量的限制,主要原因是B+树索引过大导致查询时索引无法全部加载到内存.读取 ...

  4. Java基础之第二章变量

    1. 变量介绍 变量是程序的基本组成单位 概念 变量相当于内存中一个数据存储空间的表示,可以通过变量名可以访问到变量(值). 变量使用 声明变量 int a; 赋值 a = 20; public cl ...

  5. 做个地道的c++程序猿:copy and swap惯用法

    如果你对外语感兴趣,那肯定听过"idiom"这个词.牛津词典对于它的解释叫惯用语,再精简一些可以叫"成语".想要掌握一门语言,其中的"成语" ...

  6. 【源码解析】阿里在线诊断工具greys源码

    撸起袖子加油干!开门见山! 一.源码下载 下载代码: git clone https://github.com/oldmanpushcart/greys-anatomy.git 二.源码分析 2.1 ...

  7. Python+Selenium学习笔记7 - os模块

    os模块是关于文件/目录方面的 导入语法 import os 相关方法 path.abspath()   用来获取当前路径下的文件 os.path.abspath('checkbox.html')  ...

  8. 摄像头PVD和CVD薄膜

    摄像头PVD和CVD薄膜 在FDP 的生产中,在制作无机薄膜时,可以采用的方法有两种:PVD 和CVD (将VE 和VS 归于PVD ,而ALD 归于CVD). Physical Vapor Depo ...

  9. 超轻量AI引擎MindSpore Lite

    超轻量AI引擎MindSpore Lite 揭秘一下端上的AI引擎:MindSpore Lite. MindSpore Lite是MindSpore全场景AI框架的端侧引擎,目前MindSpore L ...

  10. 编译器架构Compiler Architecture(上)

    编译器架构Compiler Architecture(上) 编译器是程序,通常是非常大的程序.它们几乎都有一个基于翻译分析综合模型的结构. CONTENTS Overview • Compiler C ...