A Simple Math Problem 矩阵打水题
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.
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.

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 矩阵打水题的更多相关文章
- HDU 1757 A Simple Math Problem (矩阵快速幂)
题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...
- HDU 1757 A Simple Math Problem(矩阵)
A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...
- HDU1757 A Simple Math Problem 矩阵快速幂
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- 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 ...
- HDU1757-A Simple Math Problem,矩阵快速幂,构造矩阵水过
A Simple Math Problem 一个矩阵快速幂水题,关键在于如何构造矩阵.做过一些很裸的矩阵快速幂,比如斐波那契的变形,这个题就类似那种构造.比赛的时候手残把矩阵相乘的一个j写成了i,调试 ...
- A Simple Math Problem(矩阵快速幂)(寒假闭关第一题,有点曲折啊)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu 1757 A Simple Math Problem (乘法矩阵)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- 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 ...
- hdu------(1757)A Simple Math Problem(简单矩阵快速幂)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
随机推荐
- Docker Swarm(六)Label 节点标签与服务约束
前言 多节点 Swarm 集群下,可能节点的配置不同(比如 CPU.内存等),部署着不同类型的服务(比如 Web服务.Job服务等),当这些服务以 Service 或者 Stack 的形式部署到集群, ...
- mysql基础之忘掉密码解决办法及恢复root最高权限办法
如果忘记了mysql的root用户的密码,可以使用如下的方法,重置root密码. 方法一: 1.停止当前mysql进程 systemctl stop mariadb 2.mysql进程停止后,使用如下 ...
- Django 自定义表名和字段名
Django 自定义表名和字段名 通过db_table和db_column自定义数据表名和字段名 假如你的数据库里已经有了一张数据表,且该表包含多个字段,你希望通过Django直接访问该数据表的各个字 ...
- Git使用总结(包含Git Bash和Git GUI的使用)(转自CSDN)
基本命令 初始化设置 配置本机的用户名和Email地址 $ git config --global user.name "Your Name" $ git config --glo ...
- 深入浅出Promise
Abstract Promise的意思是承诺(在红宝书中翻译为期约),新华字典:(动)对某项事务答应照办. Promise最早出现在Commn JS,随后形成了Promise/A规范. Promise ...
- NVIDIA 认证系统
NVIDIA 认证系统 AI 是这个时代最强大的技术,需要新一代经过调整和测试的计算机来推动其发展. 自 1 月 27 日开始,可从 NVIDIA 合作伙伴处获取用于数据中心的新型加速服务器,推动 A ...
- MinkowskiBroadcast广播
MinkowskiBroadcast广播 MinkowskiBroadcastAddition广播加法 class MinkowskiEngine.MinkowskiBroadcastAddition ...
- 35 张图带你 MySQL 调优
这是 MySQL 基础系列的第四篇文章,之前的三篇文章见如下链接 138 张图带你 MySQL 入门 47 张图带你 MySQL 进阶!!! 炸裂!MySQL 82 张图带你飞 一般传统互联网公司很少 ...
- Spring Cloud09: Config 配置中心
一.概述 什么是配置中心呢,在基于微服务的分布式系统中,每个业务模块都可以拆分成独立自主的服务,由多个请求来协助完成某个需求,那么在某一具体的业务场景中,某一个请求需要调用多个服务来完成,那么就存在一 ...
- MySQL笔记04(黑马)
今日内容 多表查询 事务 DCL 多表查询 * 查询语法: select 列名列表 from 表名列表 where.... * 准备sql # 创建部门表 CREATE TABLE dept( id ...