题目链接:

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---矩阵快速幂模板题的更多相关文章

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

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

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

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

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

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

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

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

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

  6. luoguP3390(矩阵快速幂模板题)

    链接:https://www.luogu.org/problemnew/show/P3390 题意:矩阵快速幂模板题,思路和快速幂一致,只需提供矩阵的乘法即可. AC代码: #include<c ...

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

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

  9. 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) + …… ...

随机推荐

  1. Hadoop 对MapReduce的理解

    对MapReduce的理解 客户端启动一个作业 向JobTraker请求一个JobId 将资源文件复制到HDFS上,包括Jar文件,配置文件,输入划分信息等 接收作业后,进入作业队列,根据输入划分信息 ...

  2. 洛谷P3145 [USACO16OPEN]分割田地Splitting the Field

    P3145 [USACO16OPEN]分割田地Splitting the Field 题目描述 Farmer John's NN cows (3 \leq N \leq 50,0003≤N≤50,00 ...

  3. Java Script 第一章.

    什么是Java script? JavaScript是一种基于对象的脚本语言,用于开发基于客户端和基于服务器的Internet应用程序 JavaScript是一种脚本语言(脚本语言是一种轻量级的编程语 ...

  4. Linux调优(文件系统)

    查看单个文件是否发生碎片化(被存在磁盘非连续磁盘块上) # filefrag -v /var/log/messages 查看文件系统是否存在大量碎片(会显示空闲离散的块) # dumpe2fs /de ...

  5. 利用Python的smtplib和email发送邮件

    原理 网上已经有了很多的教程讲解相关的发送邮件的原理,在这里还是推荐一下廖雪峰老师的Python教程,讲解通俗易懂.简要来说,SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本 ...

  6. redmine迁移

    (redmine使用的是bitnamiredmine一键安装,环境为apache+mysql+php) 在新环境中一键安装bitnamiredmine,安装完后,执行下面操作 1.备份原环境数据库,恢 ...

  7. 醉盏的第一篇博客-关于title的换行处理

    在处理title的时候,有时候我们想要换行,标签元素是不可以的,下面有两种特殊字符来实现 <!DOCTYPE ><html> <head runat="serv ...

  8. python3 下载 以及 练习1 以及 pycharm 专业版 安装

    下载python: https://www.python.org/downloads/release/python-365/ ########sample 1 下载pycharm 社区版本,但是web ...

  9. CentOS6.5安装配置详解

    1. 环境要求 VMWare软件: CentOS6.5对应的iso镜像文件(位数对应个人计算机位数). 2. 安装步骤 打开VMWare,文件->新建虚拟机(以下几步默认跳过即可) 命名和选择安 ...

  10. ElasticSearch搜索demo

    ElasticSearch版本:1.4.1 分词:ik jdk:1.7.67 开发工具:Eclipse 系统:win7 忙活了几天,使用ES做成,就是页面有点丑,demo页面如下: 1.搜索主页 2. ...