Ada is 5 years old, and she is learning additions. Her father writes some exercises for her.

1+11=?

2+22=?

3+33=?

“It is easy”, she writes the answer correctly. “Try to answer the following questions”,

1+11+111=?

2+22+222+2222=?

Ada scratches her head. “It’s too difficult”.

So, help Ada to compute the value of equation a+aa+aaa+... which have n items. Since the answer may be quite large, you have to module it by an integer m.

输入
Input contains at most 1000 test cases. Each test case contains only one line.

Each line will contain three integers a, n and m. (1<=a<=9, 1<=n<231-1, 1<=m<=100007).   Process to end of file.

输出
For each test cases, output the value of (a+aa+aaa+... .)%m。

样例输入
1 1 13
1 2 13
1 3 13
2 2 13
2 3 13
样例输出
1
12
6
11

12

思路:刚开始看到这个题目想的是纯模拟,但是高达了2^31-1,所以不可以,后来队友告诉我,可以构造出来一个递推的式子

|1 10 1|  |fn-1|   |fn|
|0 10 1|  |xn-1|=|xn|
|0 0   1|   |a     |  |a|

|1 10 1|^n-1   |f1|   |fn|
|0 10 1|          |x1|=|xn|
|0  0  1|          |a     |  |a|

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
/*1 10 1||fn-1|   |fn|
0 10 1||xn-1|=|xn|
0 0   1||a     |  |a|
*/
int a,b,MOD;
struct mat
{ll a[3][3]; //数组a(一般为存在幂的数组)  3为方阵的长宽
};
mat mat_mul(mat x,mat y)
{mat res;memset(res.a,0,sizeof(res.a));    //初始化for(int i=0;i<3;i++)for(int j=0;j<3;j++)for(int k=0;k<3;k++)res.a[i][j]=(res.a[i][j]+x.a[i][k]*y.a[k][j])%MOD;  //矩阵乘法return res;
}
void mat_pow(int n)
{mat c,res;c.a[0][0]=1,c.a[0][1]=10,c.a[0][2]=1;c.a[1][0]=0,c.a[1][1]=10,c.a[1][2]=1;c.a[2][0]=0,c.a[2][1]=0,c.a[2][2]=1;     //初始化赋值需要幂乘的数组memset(res.a,0,sizeof(res.a)); for(int i=0;i<3;i++) res.a[i][i]=1;      //初始化 a的n次方的数组且对角线均为1while(n){if(n&1) res=mat_mul(res,c);c=mat_mul(c,c);n=n>>1;}                                       //矩阵快速幂的运算过程int f1=a%MOD,x=a;                       //数组b的构成printf("%lld\n",(res.a[0][0]*f1+res.a[0][1]*x+res.a[0][2]*a)%MOD);
}
int main()
{int n;while(scanf("%d%d%d",&a,&n,&MOD)==3){mat_pow(n-1);}return 0;
}

Additions HNUST 1713(矩阵快速幂模板 )相关推荐

  1. POJ3070 矩阵快速幂模板

    题目:http://poj.org/problem?id=3070 矩阵快速幂模板.mod写到乘法的定义部分就行了. 别忘了 I ( ) 和 i n i t ( ) 要传引用! #include< ...

  2. 快速幂+矩阵快速幂模板

    快速..运算 快速幂 运用位运算 代码 分析 矩阵快速幂 题目 分析 代码 拓一..: 快速幂 运用位运算 强大的位运算把我搞得蒙蒙的 理解了之后我表示很喜欢!!! 代码 int power(int ...

  3. 51nod 1113 矩阵快速幂 模板题

    1113 矩阵快速幂 基准时间限制:3 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 给出一个N * N的矩阵,其中的元素均为正整数.求这个矩阵的M次方.由于M次方的计 ...

  4. How many ways?? - hdu2157(矩阵快速幂-模板)

    分析:求Map^k,刚开始没有用快速幂,TLE了   代码如下: =================================================================== ...

  5. (转)矩阵快速幂模板

    大佬博客:https://blog.csdn.net/baidu_23081367/article/details/52347256 代码: const int mat_size = 5;//矩阵大小 ...

  6. 51nod 1113 矩阵快速幂 模板

    给出一个N * N的矩阵,其中的元素均为正整数.求这个矩阵的M次方.由于M次方的计算结果太大,只需要输出每个元素Mod (10^9 + 7)的结果. Input 第1行:2个数N和M,中间用空格分隔. ...

  7. 43行代码AC_HDU-2604 Queuing(矩阵快速幂,附详细的知识讲解、模板例题)

    一道经典的矩阵快速幂模板题. 传送门1-->快速幂基本思想 传送门2-->矩阵快速幂讲解(教主传授) 传送门3.1-->HDU-1575(经典矩阵快速幂模板题1) 传送门3.2--& ...

  8. 算法分类整理+模板①:矩阵快速幂

    一直有一个想法,感觉自己很多基础算法不是很扎实,想要找个机会写一些算法的整理,顺便自己总结一些实用的模板. 最近偶然在训练赛中连续做了2道思维+矩阵快速幂的题目,碰巧有时间,就以矩阵快速幂作为这个系列 ...

  9. 快速幂 + 矩阵快速幂

    快速幂 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #define LL lo ...

  10. 蓝桥杯 算法提高 递推求值(矩阵快速幂)详解

    传送门 问题描述 已知递推公式: F(n, 1)=F(n-1, 2) + 2F(n-3, 1) + 5, F(n, 2)=F(n-1, 1) + 3F(n-3, 1) + 2F(n-3, 2) + 3 ...

最新文章

  1. 树莓派迅雷远程下载 | 树莓派小无相系列
  2. Java中使用Base64进行编码解码的工具类-将验证码图片使用Base64编码并返回给前端
  3. MyBatis逆向工程自动生成代码(附数据库表结构)
  4. 2017西安交大ACM小学期数论 [更新学号]
  5. linux中安装robot环境
  6. Python接口自动化之登录接口测试
  7. docker搭建本地 Registry
  8. 「管理数学基础」2.4 泛函分析:有界线性算子与泛函、例题
  9. 初识C语言答案,《明解C语言》第1章 初识C语言练习题答案(3页)-原创力文档...
  10. matlab2018A配置cuda,使用教程 | matlab 2018a + cuda 10.1 + vs 2017
  11. JAVA学习笔记之J2SDK主要包介绍 (马士兵 教学视频)
  12. 管理hiberfil.sys文件与pagefile.sys文件释放C盘
  13. 2.19 serenity
  14. 玻纤效应对skew的影响(一)
  15. 阿里云建站之模板建站的核心优势有哪些?
  16. 多传感器融合track fusion
  17. Java小白自学7:选择结构练习题
  18. C#,蛇梯问题(Snake and Ladder Problem)的算法与源代码
  19. Python绘制万花筒
  20. Color a Tree

热门文章

  1. UVA 12307 Smallest Enclosing Rectangle(旋转卡壳)
  2. swagger 怎么去掉get delete_橡皮擦英文单词怎么读
  3. Delphi XE2控件安装方法
  4. 函数6:lambda 表达式
  5. 记录下SpringBoot父子工程使用jib构建docker镜像的过程(跳过多余模块)
  6. 浏览器被劫持怎么解决?关于浏览器被劫持主页的处理方法
  7. 视频教程-计算机二级(VB)-计算机等级考试
  8. SRVCC关键场景及Log分析
  9. 完美解决Window11任务栏合并图标的问题。
  10. IDEA - 如何安装Statistic代码统计插件?