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) + …… ...
随机推荐
- 基础篇---memcache
十分钟学会memcache,比你想象的要简单 转发:https://baijiahao.baidu.com/s?id=1588816843517136163&wfr=spider&fo ...
- Unity Shader着色器优化
https://mp.weixin.qq.com/s?__biz=MzU5MjQ1NTEwOA==&mid=2247493518&idx=1&sn=c51b92e9300bcf ...
- 《OD学spark》20160924scala基础
拓展: Hadoop 3.0 NameNode HA NameNode是Active NameNode是Standby可以有多个 HBase Cluster 单节点故障? HBaster -> ...
- iOS通过SocketRocket实现websocket的即时聊天
之前公司的即时聊天用的是常轮循,一直都觉得很不科学,最近后台说配置好了socket服务器,我高兴地准备用asyncsocket,但是告诉我要用websocket,基于HTML5的,HTML5中提出了一 ...
- Idea提示没有符号类错误解决
将提示没有符号类的文件打开,右键单独编译一次,再重新打包即可解决
- OJDBC版本区别:ojdbc14.jar,ojdbc5.jar和ojdbc6.jar的区别
classes12.jar - for Java 1.2 and 1.3ojdbc14.jar - for Java 1.4 and 1.5ojdbc5.jar - for Java 1.5ojdbc ...
- 洛谷 P4397 [JLOI2014]聪明的燕姿 / TOPOI 测验1315, 问题E: 1935: 聪明的燕姿 解题报告
题目链接 : 1. 洛谷 2.topoi . 大致题意:输入一个数s,找出所有约数和为s的数 关于一个数的约数和求法: 一个>1的整数可以被分解为多个 质数 的乘方,设数 s = p1k1 * ...
- Knight Tournament (set)
Hooray! Berl II, the king of Berland is making a knight tournament. The king has already sent the me ...
- Unittest组织用例的姿势
本文我们将会讲解Python Unittest 里组织用例的5种姿势. 环境准备: python 3.0以上 python requests库 小编的环境: python 3.6.4 一.TestLo ...
- Base64Utils
package com.yundaex.common.crypto.base64; import java.io.ByteArrayInputStream; import java.io.ByteAr ...