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 >= 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 .
解题思路:
构建矩阵

直接用矩阵快速幂模板求解
注意,小于10的时候不能直接输出k,要输出k%m
#include<bits/stdc++.h>
using namespace std;
const int maxn = + ;
int MOD;
struct Mat
{
int a[maxn][maxn];
int n, m;//n为行数,m为列数
Mat(int n, int m):n(n), m(m)
{
memset(a, , sizeof(a));
}
void init()
{
for(int i = ; i < n; i++)a[i][i] = ;//初始化成单位矩阵
}
void output()
{
for(int i = ; i < n; i++)
{
for(int j = ; j < m; j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
}
};
Mat mul(Mat a, Mat b)//矩阵乘法
{
Mat tmp(a.n, b.m);//矩阵乘法结果矩阵行数为a的行数,列数为b的列数
for(int i = ; i < a.n; i++)
{
for(int j = ; j < b.m; j++)
{
for(int k = ; k < a.m; k++)//a.m == b.n(乘法的前提条件)
{
tmp.a[i][j] += (a.a[i][k] * b.a[k][j] % MOD);
tmp.a[i][j] %= MOD;
}
}
}
return tmp;
}
Mat pow(Mat a, int n)
{
Mat tmp(a.n, a.m);
tmp.init();
while(n)
{
if(n & )tmp = mul(tmp, a);
n /= ;
a = mul(a, a);
}
return tmp;
}
int main()
{
int n, m;
while(cin >> n >> m)
{
Mat a(, );
Mat b(, );
//对a和b进行初始化
for(int i = ; i < ; i++)cin >> a.a[][i];
for(int i = ; i < ; i++)a.a[i][i - ] = ;
for(int i = ; i < ; i++)b.a[i][] = - i;
MOD = m;
//a.output();
//b.output();
if(n < )
{
cout<<(n % m)<<endl;
}
else
{
Mat ans = pow(a, n - );
//ans.output();
ans = mul(ans, b);
cout<<ans.a[][]<<endl;
}
}
}
hdu-1757 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_矩阵快速幂
题意:略 简单的矩阵快速幂就行了 #include <iostream> #include <cstdio> #include <cstring> using na ...
- 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 ...
- luoguP3390(矩阵快速幂模板题)
链接:https://www.luogu.org/problemnew/show/P3390 题意:矩阵快速幂模板题,思路和快速幂一致,只需提供矩阵的乘法即可. AC代码: #include<c ...
- hdu 1757 A Simple Math Problem (矩阵快速幂)
Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x. If x >= 10 ...
- 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(矩阵快速幂)
题目链接 题意 :给你m和k, 让你求f(k)%m.如果k<10,f(k) = k,否则 f(k) = a0 * f(k-1) + a1 * f(k-2) + a2 * f(k-3) + …… ...
随机推荐
- python文件名匹配
待匹配文件:#FY3D_IPMNT_GBAL_L1_20180516_0003_030KM_MS.HDF 干扰文件:#FY3D_IPMNT_GBAL_L1_20180516_0003_030KM_MS ...
- 解读人:李琼,Metabolic profiling of tumors, sera and skeletal muscles from an orthotopic murine model of gastric cancer associated-cachexia(胃癌相关恶病质的原位小鼠模型中肿瘤,血清和骨骼肌的代谢谱分析)
发表时间:(2019年4月) IF:3.950 单位: 厦门大学 厦门理工大学 物种:小鼠的肿瘤,血清和骨骼肌 技术:核磁共振代谢组学分析 一. 概述:(用精炼的语言描述文章的整体思路及结果) 本研究 ...
- springmvc ajax 简单例子
1.控制器曾 @Controller public class AjaxController { @RequestMapping("/ajax") public void ajax ...
- uuid安装 插件安装
yum -y install uuid uuid-devel 安装uuid包tar -zxvf uuid-1.6.1.tar.gzcd uuid-1.6.1./configuremakemake in ...
- [USACO07DEC]观光奶牛Sightseeing Cows 二分答案+判断负环
题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...
- PHP下载远程图片的几种方法总结
1. 使用file_get_contents function dlfile($file_url, $save_to) { $content = file_get_contents($file_url ...
- ELK系列(2) - Kibana怎么修改日期格式Date format
问题 Kibana在创建Index Patterns的时候,可以选择某个date类型的field作为排序字段.之后在Discover里打开对应的index,会发现这个date类型的field的格式显示 ...
- javascript的模块发展
谨以此文记录了解js模块的过程 随着ES6的出现,js模块已经成为正式的标准了.曾经为了解决js模块问题而发展起来的民间秘籍,requireJs(AMD).SeaJs(CMD).Node(Common ...
- JavaScript和jquery中的宽度
Javascript: 网页可见区域宽: document.body.clientWidth 网页可见区域高: document.body.clientHeight 网页可见区域宽: document ...
- Qt学习之网络编程(一)
一些说明 学了有一段时间的python了,小项目做了不少,最近由于项目需要,所以要回归老本行了,开始重点突击C++和qt.python的网络爬虫系列有时间就更吧. 获取本机网络信息 在网络应用中,经常 ...