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

思路:矩阵快速幂模板题,不过因为刚刚入门矩阵快速幂,所以经常把数组f存反,导致本地错误一晚,差点心态爆炸……

代码实现如下:

 #include <cstdio>
#include <cstring> int k, m;
int a[], f[], mp[][]; void mulself(int a[][]) {
int c[][];
memset(c, , sizeof(c));
for(int i = ; i < ; i++) {
for(int j = ; j < ; j++) {
for(int k = ; k < ; k++) {
c[i][j] = (c[i][j] + (long long)a[i][k] * a[k][j]) % m;
}
}
}
memcpy(a, c, sizeof(c));
} void mul(int f[], int a[][]) {
int c[];
memset(c, , sizeof(c));
for(int i = ; i < ; i++) {
for(int j = ; j < ; j++) {
c[i] = (c[i] + (long long)f[j] * a[j][i] ) % m;
}
}
memcpy(f, c, sizeof(c));
} int main() {
while(~scanf("%d%d", &k, &m)) {
for(int i = ; i < ; i++) {
scanf("%d", &a[i]);
}
for(int i = ; i < ; i++) {
f[i] = - i;
}
if(k <) {
printf("%d\n", f[k]);
continue;
}
memset(mp, , sizeof(mp));
for(int i = ; i < ; i++) {
mp[i][] = a[i];
}
for(int i = ; i < ; i++) {
mp[i][i + ] = ;
}
k = k - ;
for(; k; k >>= ) {
if(k & ) mul(f, mp);
mulself(mp);
}
printf("%d\n", f[]);
}
return ;
}

A Simple Math Problem(矩阵快速幂)的更多相关文章

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

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

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

    题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...

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

  4. hdu 1757 A Simple Math Problem_矩阵快速幂

    题意:略 简单的矩阵快速幂就行了 #include <iostream> #include <cstdio> #include <cstring> using na ...

  5. BestCoder Round #29——A--GTY's math problem(快速幂(对数法))、B--GTY's birthday gift(矩阵快速幂)

    GTY's math problem Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  6. HDU 1757 A Simple Math Problem(矩阵)

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

  7. hdu-1757 A Simple Math Problem---矩阵快速幂模板题

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1757 题目大意: 求递推式第k项模m If x < 10 f(x) = x.If x > ...

  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 - 3521 An easy Problem(矩阵快速幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=3521 题意 对于矩阵A,求e^A的值. 分析 这个定眼一看好像很熟悉,就是泰勒展开,可惜自己的高数已经还给老师了 ...

  10. hdu 6182A Math Problem(快速幂)

    You are given a positive integer n, please count how many positive integers k satisfy kk≤nkk≤n.  Inp ...

随机推荐

  1. 转 【.NET平台下使用MongoDB入门教程】

    目录 一.了解MongoDB 二.MongoDB特点 三.安装及常用命令 3.1 下载安装 3.2 启动服务器 3.3 常用操作 3.4 其他命令 3.5 做成windows服务 四.批处理程序开启M ...

  2. 原生js 自定义confirm

    本文参考博客园另一篇文章:https://www.cnblogs.com/hzj680539/p/5374052.html,在此感谢. 在实际开发当中,考虑到原生js组件,包括alert.confir ...

  3. Ubuntu安装配置JDK、Tomcat、SVN服务器

    一.配置jdk 1.下载JDK http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html 注:笔者是直 ...

  4. [OS] 死锁相关知识点以及银行家算法详解

    因此我们先来介绍一下死锁: 死锁特征 当出现死锁时,进程永远不能完成,并且系统资源被阻碍使用,阻止了其他作业开始执行.在讨论处理死锁问题的各种方法之前,先深入讨论一下死锁的特征. ·必要条件 (1)互 ...

  5. BIO、NIO、AIO通信机制

    一.BIO的理解 首先我们通过通信模型图来熟悉下BIO的服务端通信模型:采用BIO通信模型的服务端,通常由一个独立的Acceptor线程负责监听客户端的连接,它接收到客户端的连接请求之后为每个客户端创 ...

  6. SQL局部变量

    声明局部变量 局部变量的声明需要使用declare 语句.并且必须以@开头 declare { @varaible_name datatype[,...n] } varaible_name :局部变量 ...

  7. 在Linux上编译TCMalloc

    TCMalloc(Thread-Caching Malloc)与标准glibc库的malloc实现一样的功能,但是TCMalloc在效率和速度效率都比标准malloc高很多.TCMalloc是goog ...

  8. CMD命令提示符

    mspaint  画图板 notepad  打开记事本 write  写字板 calc.exe  计算器 control.exe  控制面板 osk  打开屏幕键盘 rononce -p ----15 ...

  9. CentOS 挂载(U盘NTFS格式,新硬盘,增加交换分区,扩展根分区等)

    1.挂载fat或者fat32分区的U盘 如果是用VM安装的linux,在vm里挂载U盘有两个前提: 第一,主机里的service要启动: 第二,U盘是连接到虚拟机,而不是主机,需要确认这点: 2.使用 ...

  10. Signal函数

    Signal函数: 这个函数是一种系统调用,就是告诉系统发生中断的时候用该干嘛.第一个参数就是信号的编号,第二个参数就是信号的指针. 原型: #include <signal.h> voi ...