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 * ...
随机推荐
- C# 因缺少CategoryName,而未能初始化 的解决办法
群里一小伙伴在开发APP时遇到了问题,便截图提问 一.傻瓜式解决办法: 删除: ((System.ComponentModel.ISupportInitialize)(this.performance ...
- 由于服务主机:DCOM服务进程占用过多CPU,导致系统卡死
最近在使用电脑的时候,总是出现电脑死机,而且鼠标也是经常卡在那里不动了,开始以为是鼠标的问题,还换了个鼠标(飙泪中),这还是一个血的教训啊!!!之后打开任务管理器发现CPU占用已经达到100%,而且一 ...
- centos 7下安装mysql-8.0
本篇文章主要介绍在centos7 环境下安装mysql8.0并设置为开机自启. 安装步骤 1.配置yum源 首先在 https://dev.mysql.com/downloads/repo/yum/ ...
- Dynamics 365-关于Solution的那些事(二)
接着上一篇的说,现在有一个已知前提:Solution的增量特性.然后我们再思考这么一个场景,项目开发过程中,存在多次迭代的情况,每次迭代可能涉及到的solution是同一个,唯一区别的,就是solut ...
- Microsoft SQL Server 2016 RC3 安装
首先下载SQL Server 2016 RC3 安装iso 下载链接 ed2k://|file|cn_sql_server_2016_rc_3_x64_dvd_8566578.iso|24648232 ...
- axis根据wsdl生成java客户端代码
根据wsdl生成java客户端代码有多个方法,其中使用axis生成的代码比较友好,也是经常用的一种方法.首先下载axis jar包:axis-bin-1_4.zip 官方地址:http://ws.Ap ...
- Java中单例模式的几种实现
目录 懒汉式单例 简单版本 synchronized版本 双重检查(Double-Check)版本 volatile 饿汉式单例 实现1 其他实现方式 静态内部类-Effective Java 枚举- ...
- matplotlib箱线图与柱状图比较
代码: # -*- coding: utf-8 -*- """ Created on Thu Jul 12 16:37:47 2018 @author: zhen &qu ...
- iBatis第二章:搭建一个简单的iBatis开发环境
使用 iBatis 框架开发的基本步骤如下:1.新建项目(iBatis是持久层框架,可以运用到java工程或者web工程都可以) 这里我们建立一个 web 工程测试. 2.导入相应的框架 jar 包 ...
- Linux(Manjaro) - Docker - MySQL 安装配置
Linux(Manjaro) - Docker - MySQL 安装配置 拉取mysql镜像 # 使用网易的 MySQL 镜像地址 docker pull hub.c.163.com/library/ ...