题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757

在吴神的帮助下才明白如何构造矩阵,还是好弱啊。

此处盗一张图

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6
 7 using namespace std;
 8
 9 typedef long long ll;
10
11 const int N = 10;
12
13 ll k,m;
14 int a[10];
15
16 struct matrix
17 {
18     ll mat[N][N];
19 };
20 matrix base;
21 void initial()
22 {
23     memset(base.mat,0,sizeof(base.mat));
24     for(int i=0; i<N; i++)
25         base.mat[0][i]=a[i];
26     for(int i=1; i<N; i++)
27         for(int j=0; j<N; j++)
28             if(i==j+1)
29                 base.mat[i][j]=1;
30 }
31 matrix multi(matrix a,matrix b)
32 {
33     matrix tmp;
34     memset(tmp.mat,0,sizeof(tmp.mat));
35     for(int i=0; i<N; i++)
36         for(int j=0; j<N; j++)
37         {
38             for(int k=0; k<N; k++)
39                 tmp.mat[i][j]=tmp.mat[i][j]+a.mat[i][k]*b.mat[k][j]%m;
40             tmp.mat[i][j]%=m;
41         }
42     return tmp;
43 }
44
45 ll cal(ll n)
46 {
47     matrix ans;
48     memset(ans.mat,0,sizeof(ans.mat));
49     for(int i=0; i<N; i++)
50         for(int j=0; j<N; j++)
51             if(i==j)
52                 ans.mat[i][j]=1;
53     while(n)
54     {
55         if(n&1)
56             ans=multi(base,ans);
57         base=multi(base,base);
58         n>>=1;
59     }
60
61     ll sum=0;
62     for(int i=0; i<N; i++)
63         sum+=ans.mat[0][i]*(N-i-1)%m;
64     return sum%m;
65 }
66 int main()
67 {
68     while(~scanf("%lld%lld",&k,&m))
69     {
70         for(int i=0; i<N; i++)
71             scanf("%d",&a[i]);
72         if(k<10)
73             printf("%lld\n",k%m);
74         else
75         {
76             initial();
77             printf("%lld\n",cal(k-9));
78         }
79     }
80     return 0;
81 }

转载于:https://www.cnblogs.com/ExcuseMe/p/5557624.html

HDU 1757 A Simple Math Problem (矩阵快速幂)相关推荐

  1. HDU - 1757 A Simple Math Problem(矩阵快速幂,水题)

    题目链接:点击查看 题目大意:实现公式: f(x)=x,x<10 f(x)=a0*f(x-1)+a1*f(x-2)+--+a9*f(x-10) 题目给出a0~a9,一个n和一个m,要求输出f(n ...

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

  3. 43行代码AC——HDU 1757 A Simple Math Problem(矩阵快速幂,附快速幂讲解)

    一道经典的矩阵快速幂模板题. 传送门1-->快速幂基本思想 传送门2-->矩阵快速幂讲解(教主传授) 代码(去掉空行43行) #include<iostream> #inclu ...

  4. hdu 1757 A Simple Math Problem 构造矩阵

    题意:函数f(x), 若 x < 10 f(x) = x. 若 x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + ...

  5. HDU 1757 A Simple Math Problem

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

  6. HDU - 1757 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 * ...

  7. HDOJ 1757 A Simple Math Problem(矩阵快速幂)

    2018-5-24 简单的矩阵快速幂问题,重点是如何找到对应关系. f(10) a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 f(9) f(9) 1 0 0 0 0 0 0 0 0 0 ...

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

  9. HDU - 5974 A Simple Math Problem 题解

    D - Simple Math Problem 戳上方进入原题哟~ 题目大意 给一个数 a,b. 让你求满足一下条件的 X,Y: X+Y = a LCM(X,Y) = b 解析 题目给出时间限制1s, ...

最新文章

  1. android地址格式转换,Android(安卓)时间戳和日期之间的转化
  2. 【C#学习笔记】退出程序
  3. springmvc 配置 fastjson解析器
  4. SSL-ZYC 1760 商店选址问题
  5. 透过表象看本质!?之二数据拟合
  6. 【POJ 2482】【扫描线问题】Stars in Your Window【包星星问题】
  7. Shiro的详细简介解释(快速搭建官网解释代码)
  8. 从excel表格生成ArcGIS Pro样式符号
  9. 能力与爱好 我能熟练使用计算机,关于2020大学生简历自我评价精选5篇(心选优品)...
  10. java评论回复表设计_评论回复功能数据表设计
  11. java overload_Java方法重载Overload原理及使用解析
  12. bittorrent_面向初学者的BitTorrent:充分利用Internet连接
  13. python中for循环的代码_Python中的九九乘法表(for循环)
  14. AutoSAR实验视频讲解:相关资料下载
  15. 高精度4位压缩法原理与实现
  16. Virtual Box虚拟机文件瘦身处理
  17. 从材料硕士到算法工程师的转行之路
  18. 转载:discuz代码解析(一、初始化应用的过程)
  19. 国产信创Linux桌面系统比较:软件包格式及软件管理、桌面环境及桌面应用
  20. CAKEPHP 约定

热门文章

  1. 拖欠水费可能影响个人征信,大家怎么看?
  2. 贷款审批到底会考核哪些内容?
  3. 单片机小白学步系列(十六) 单片机/计算机系统概述:模块化思想
  4. sql 相加_SQL经典题型
  5. 深信服上网行为管理开启snmp_编写SHELL脚本在CACTI中来实现监控深信服上网行为管理的CPU和内存使用情况...
  6. python写mapreduce_用python写MapReduce函数——以WordCount为例
  7. win10兼容模式怎么设置_win10护眼模式关不掉怎么办
  8. Games101现代图形学入门Lecture 4: Transformation Cont知识点总结
  9. Web程序中使用EasyUI时乱码问题
  10. PostgreSQL安装和简单配置