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) + …… ...
随机推荐
- AOP常用注解
1.@Aspect 配置切面Bean,和<bean.../>元素进行配置无区别,一样支持依赖注入来配置属性值: 如果启动了Spring的"零配置"特性,一样可以让Spr ...
- c语言数组相关的计算
1.数组的创建:元素类型 数组名 [常量或者常量表达式] 如:int arr1[10];注:即使是被const修饰的变量也不能作为[]中的内容,它本质上依然属于变量,只是具有常量属性2.数组的初始化: ...
- [Xcode 实际操作]三、视图控制器-(12)在Storyboard中使用集合控件
目录:[Swift]Xcode实际操作 本文将演示集合控件在故事板中的使用. 在控制器根视图上点击鼠标,以选择该根视图. 现在往根视图中添加一个集合视图. 点击[库面板]图标,打开控件库面板 在控件库 ...
- 关于在SSM框架下使用PageHelper
首先,如果各位在这块配置和代码有什么问题欢迎说出来,我也会尽自己最大的能力帮大家解答 这些代码我都是写在一个小项目里的,项目的github地址为:https://github.com/Albert-B ...
- 如何理解javascript中的同步和异步
javascript语言是一门“单线程”的语言,不像java语言,类继承Thread再来个thread.start就可以开辟一个线程,所以,javascript就像一条流水线,仅仅是一条流水线而已,要 ...
- BeanFilterUtil
package com.yundaex.utility.bean.filter; import java.util.ArrayList; import java.util.List; import o ...
- .db文件打开方式
有时在工作中,数据库格式db后缀的格式,直接是打不开的,所以我这里使用了数据库管理工具,步骤如下 1. 在电脑安装 Navicat Premium,安装后在桌面生成图标,点击图标打开程序. 2.打开程 ...
- c#spinLock使用
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u011915028/article/details/53011811 一下解释摘自msdn ...
- sql server sql语句
添加联合唯一索引 create unique index 索引名 on 表名(列名1,列名2……)
- ORA-02298: 无法验证 (约束) - 未找到父项关键字 解决办法
--在用PL/SQL导入表数据的时候报错 ORA-02298: 无法验证 (PNET.POST_CLOB_FK) - 未找到父项关键字 --发现是启用外键约束时报的错alter table DM_VO ...