题目

A Simple Math Problem

解析

矩阵快速幂模板题

构造矩阵

\[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a_5&a_6&a_7&a_8&a_9\\
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 (矩阵快速幂)的更多相关文章

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

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

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

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

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

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

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

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

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

  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 (矩阵快速幂,简单)

    题目 也是和LightOJ 1096 和LightOJ 1065 差不多的简单题目. #include<stdio.h> #include<string.h> #include ...

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

    <font color = red , size = '4'>下列图表转载自 efreet 链接:传送门 题意:给出递推关系,求 f(k) % m 的值, 思路: 因为 k<2 * ...

随机推荐

  1. ado.net的简单数据库操作(二)之封装SqlHelperl类

    今天我书接上回,接着昨天的ado.net的数据库操作的相关知识来讲哈! 从上篇文章给出的实例来看,你一定会发现,操作数据库其实还挺麻烦的,就连一个最简单的数据库操作语句都要包括 定义数据库连接字符串. ...

  2. iframe 父页面调用子页面的vue方法

                    父页面代码:            html: <div id="app"> //省略业务代码x行..... <iframe sr ...

  3. SuperMap iObject入门开发系列之一组件式GIS开发平台介绍

    本文是一位好友“炀炀”授权给我来发表的,介绍都是他的研究成果,在此,非常感谢.平台介绍:SuperMap iObjects Java/.NET 是面向GIS应用系统开发者的组件式GIS开发平台,具有强 ...

  4. Odoo / PS Cloud12版本中,产品变体功能如何使用

    场景: 产品:陶瓷马克杯 产品颜色变体:红色.蓝色.白色 产品尺寸变体:10CM.12CM.15CM 每个变体都有不同价格维度 odoo / PS Cloud 专业实施开发 EMAIL:1715860 ...

  5. 生鲜配送管理系统_升鲜宝V2.0 价格组功能 操作说明_15382353715

    价格组功能是B端供应链系统,必不可少的一个功能,其主要实现不同的客户不同的价格,B端系统有一个最大的不同就是,有些商品后台下单人员能看到的.有些商品在销售的那一瞬间,还不知道价格.所以这些商品只有后台 ...

  6. [Python] wxPython 高防Windows10记事本 (end...)

    1.开始 接触Python 也有一段时间了,o.o ,断断续续加起来没几天. 一般新学习一门新语言,除了必先输出一个 Hello World 外,都会以模拟 Windows 记事本来写一个结合自己想法 ...

  7. 【Spring Cloud笔记】Eureka注册中心增加权限认证

    在Spring Cloud通过Eureka实现服务注册与发现时,默认提供web管理界面,但是如果在生产环境暴露出来,会存在安全问题.为了解决这个问题,我们可以通过添加权限认证进行控制,具体步骤如下: ...

  8. js得到规范的时间格式函数,并调用

    1.js得到规范的时间格式函数 Date.prototype.format = function(fmt) { var o = { "M+" : this.getMonth()+1 ...

  9. Linux(CentOS7)压缩和解压缩war包、tar包、tar.gz包命令

    一.Linux版本 二.解压缩.tar.gz包到当前目录 tar -xzvf apache-tomcat-7.0.90.tar.gz 三.将指定文件压缩成.tar.gz包 tar -czf apach ...

  10. Windows -- 从注册表删除IE浏览器加载项

    Windows -- 从注册表删除IE浏览器加载项 1.  一部分加载项从注册表以下位置直接删除 2.  一部分扩展项从注册表以下位置直接删除