HDU 1757 A Simple Math Problem (矩阵快速幂)
题目
解析
矩阵快速幂模板题
构造矩阵
1&0&0&0&0&0&0&0&0&0\\
0&1&0&0&0&0&0&0&0&0\\
0&0&1&0&0&0&0&0&0&0\\
0&0&0&1&0&0&0&0&0&0\\
0&0&0&0&1&0&0&0&0&0\\
0&0&0&0&0&1&0&0&0&0\\
0&0&0&0&0&0&1&0&0&0\\
0&0&0&0&0&0&0&1&0&0\\
0&0&0&0&0&0&0&0&1&0\\
\end{bmatrix}^{n-9}
\begin{bmatrix}f_{n-1}\\f_{n-2}\\f_{n-3}\\f_{n-4}\\f_{n-5}\\f_{n-6}\\f_{n-7}\\f_{n-8}\\f_{n-9}\\f_{n-10}
\end{bmatrix}=\begin{bmatrix}f{n}\\f_{n-1}\\f_{n-2}\\f_{n-3}\\f_{n-4}\\f_{n-5}\\f_{n-6}\\f_{n-7}\\f_{n-8}\\f_{n-9}
\end{bmatrix}\]
然后套矩阵快速幂就完了。
代码
因为我的快速幂是直接用构造好的矩阵,不用再构造一个单位矩阵,所以幂的次数少1
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 110;
int n, m;
class matrix {
public :
int a[N][N];
matrix() {
memset(a, 0, sizeof(a));
}
matrix operator * (const matrix &oth) const {
matrix ret;
memset(ret.a, 0, sizeof(ret.a));
for (int i = 1; i <= 10; ++i)
for (int j = 1; j <= 10; ++j)
for (int k = 1; k <= 10; ++k)
ret.a[i][j] = (ret.a[i][j] % m + (this->a[i][k] * oth.a[k][j]) % m) % m;
return ret;
}
} init;
template<class T>inline void read(T &x) {
x = 0; int f = 0; char ch = getchar();
while (!isdigit(ch)) f |= (ch == '-'), ch = getchar();
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
x = f ? -x : x;
return;
}
matrix qpow(matrix a, int b) {
matrix ans = init;
while (b) {
if (b & 1) ans = ans * a;
b >>= 1, a = a * a;
}
return ans;
}
int f[N][N], ans;
signed main() {
while (scanf("%lld%lld", &n, &m) != EOF) {
ans = 0;
memset(init.a, 0, sizeof(init.a));
for (int i = 1; i <= 10; ++i) read(init.a[1][i]);
if (n <= 9) {
printf("%lld\n", n);
continue;
}
for (int i = 2; i <= 10; ++i) init.a[i][i - 1] = 1;
init = qpow(init, n - 10);
for (int i = 1; i <= 10; ++i) ans += init.a[1][i] * (10 - i);
printf("%lld\n", ans % m);
}
}
HDU 1757 A Simple Math Problem (矩阵快速幂)的更多相关文章
- 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 ...
- 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) + …… ...
- 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 (矩阵快速幂,简单)
题目 也是和LightOJ 1096 和LightOJ 1065 差不多的简单题目. #include<stdio.h> #include<string.h> #include ...
- HDU 1757 A Simple Math Problem( 矩阵快速幂 )
<font color = red , size = '4'>下列图表转载自 efreet 链接:传送门 题意:给出递推关系,求 f(k) % m 的值, 思路: 因为 k<2 * ...
随机推荐
- K2制作流程
K2流程制作注意事项 1:分析需求 2:实施 步骤1:绘制流程图 步骤2:添加datafield[必备:ActJumped IsPass] 步骤3:添加线规则(如下图所示,在添加完毕规则之后,再给同 ...
- FineUIMvc表格数据库分页,使用CYQ.Data组件
首先看下前台 View 的定义: @(F.Grid() .EnableCheckBoxSelect(true) .Width(850) .ShowHeader(true) .ShowBorder(tr ...
- c#中如何使用到模糊查询
c#中如何使用到模糊查询,先举个最简单实用的例子,可在vs控制台应用程序中输出: 定义实体类: public class Student { public int ...
- 设计模式 - 单例模式(Singleton Pattern)
单例模式 介绍 模式:创建型 意图:保证一个类只有一个实例,并提供一个访问它的全局访问点 解决:一个全局使用的类频繁地创建与销毁 场景: 唯一序列号 web中的计数器 I/O与数据库的连接 …… 实现 ...
- Math.floor(Math.random()*3+1)
Math.random():获取0~1随机数 Math.floor() method rounds a number DOWNWARDS to the nearest integer, and ret ...
- 从0到1搭建AI中台
文章发布于公号[数智物语] (ID:decision_engine),关注公号不错过每一篇干货. 转自 | 宜信技术学院 作者 | 井玉欣 导读:随着“数据中台”的提出和成功实践,各企业纷纷在“大中台 ...
- node配置微信小程序解密消息以及推送消息
上一篇文章介绍过 微信小程序配置消息推送,没有看过的可以先去查看一下,这里就直接去把那个客服消息接口去解密那个消息了. 在这里我选择的还是json格式的加密. 也就是给小程序客服消息发送的消息都会被微 ...
- Jmeter简单回顾
之前公众号推文一上手就分享如何测接口, 其实忽略了一些概念性的东西, 今天来给大家拾遗补缺, 做个回顾吧. 一. JMeter介绍 jmeter能做什么,来自官网的解释: Ability to loa ...
- Neo4j 全文检索
全文检索基本概念 搜索 搜索这个行为是用户与搜索引擎的一次交互过程,用户需要找一些数据,他提供给搜索引擎一些约束条件.搜索引擎通过约束条件抽取一些结果给用户 搜索引擎 搜索引擎存在的目的是存储,查找和 ...
- Java基础系列--01_基础类型
J2SE.J2ME.J2EE分别指什么? J2SE 基础版,桌面应用. J2ME 微型版,手机开发.(android,ios) J2EE 企业版,所有浏览器访问的应用程序. 注意:JDK5以后改名 J ...