A Simple Math Problem(矩阵快速幂)
题目链接: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(矩阵快速幂)的更多相关文章
- HDU1757 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 (矩阵快速幂)
题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...
- 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 ...
- hdu 1757 A Simple Math Problem_矩阵快速幂
题意:略 简单的矩阵快速幂就行了 #include <iostream> #include <cstdio> #include <cstring> using na ...
- 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 ...
- HDU 1757 A Simple Math Problem(矩阵)
A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...
- 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 > ...
- 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 & ...
- HDU - 3521 An easy Problem(矩阵快速幂)
http://acm.hdu.edu.cn/showproblem.php?pid=3521 题意 对于矩阵A,求e^A的值. 分析 这个定眼一看好像很熟悉,就是泰勒展开,可惜自己的高数已经还给老师了 ...
- hdu 6182A Math Problem(快速幂)
You are given a positive integer n, please count how many positive integers k satisfy kk≤nkk≤n. Inp ...
随机推荐
- python学习笔记06:操作文件
调用内置的open函数打开文件,传递两个参数:文件路径(绝对路径或相对路径),打开模式('r':读,'r+':读写,'w':写,'b':二进制): f = open('data.txt','w') f ...
- lintcode-34-N皇后问题 II
34-N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 标签 递归 思路 参考http://www.cnblogs.com ...
- Qt窗口及控件-QTreeview/QTableView排序问题
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt-QTreeview/QTableView排序问题 本文地址:http://tec ...
- printf以及各种变种
int printf(char *format, [argument]); 其向终端(显示器.控制台等)输出字符 int fprintf(FILE*stream, const char*format, ...
- 第70天:jQuery基本选择器(一)
一.jQuery基本选择器 jQuery是javascript的一个库,包含多个可重用的函数,用来辅助我们简化javascript开发 jQuery能做的javascipt都能做到,而javascri ...
- 【Python】ORM框架SQLAlchemy的使用
ORM和SQLAlchemy简介 对象关系映射(Object Relational Mapping,简称ORM),简单的来说,ORM是将数据库中的表与面向对象语言中的类建立了一种对应的关系.然后我们操 ...
- 【MVC】ASP.Net MVC 4项目升级MVC 5的方法
1.备份你的项目 2.从Web API升级到Web API 2,修改global.asax,将 ? 1 WebApiConfig.Register(GlobalConfiguration.Config ...
- RT-thread内核之内核对象模型
RT-Thread的内核对象模型是一种非常有趣的面向对象实现方式.由于C语言更为面向系统底层,操作系统核心通常都是采用C语言和汇编语言混合编写而成.C语言作为一门高级计算机编程语言,一般被认为是一种面 ...
- CF757G Can Bash Save the Day?
CF757G Can Bash Save the Day? #include<bits/stdc++.h> #define RG register #define IL inline #d ...
- BZOJ5312:冒险——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=5312 Kaiser终于成为冒险协会的一员,这次冒险协会派他去冒险,他来到一处古墓,却被大门上的守护 ...