HDU 1757 A Simple Math Problem( 矩阵快速幂 )
<font color = red , size = '4'>下列图表转载自 efreet
链接:传送门
题意:给出递推关系,求 f(k) % m 的值,
思路:
因为 k<2 * 10^9 , m < 10^5,O(n)算法应该是T掉了,当 k >= 10 时 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10),可以理解为这是两个行列是乘积的值,经下面分析可知用矩阵快速幂可搞
下列三个表分别命名为矩阵0,矩阵1,矩阵2。
| fk | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| fk-1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| fk-2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| fk-3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| fk-4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| fk-5 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| fk-6 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| fk-7 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| fk-8 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| fk-9 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
等于
| a0 | a1 | a2 | a3 | a4 | a5 | a6 | a7 | a8 | a9 |
| 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 |
乘以
| fk-1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| fk-2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| fk-3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| fk-4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| fk-5 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| fk-6 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| fk-7 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| fk-8 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| fk-9 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| fk-10 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
- 经过观察发现,当 k >= 10 时 f(k) = [ 矩阵1 ]^(k-9) * [ 矩阵2 ]
/*************************************************************************
> File Name: hdu1757.cpp
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年05月02日 星期二 19时25分30秒
************************************************************************/
#include<bits/stdc++.h>
using namespace std;
const int maxn = 11;
int MOD;
#define ll long long
#define mod(x) ((x)%MOD)
struct mat{
int m[maxn][maxn];
}unit;
int n;
mat operator * (mat a,mat b){
mat ret;
ll x;
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
x = 0;
for(int k=0;k<10;k++)
x += mod( (ll)a.m[i][k]*b.m[k][j] );
ret.m[i][j] = mod(x);
}
}
return ret;
}
// 初始化单位阵
void init_unit(){
for(int i=0;i<10;i++)
unit.m[i][i] = 1;
return;
}
mat pow_mat(mat a,ll x){
mat ret = unit;
while(x){
if(x&1) ret = ret*a;
a = a*a;
x >>= 1;
}
return ret;
}
int main(){
mat a , f;
int aa[10];
ll k;
init_unit();
memset(f.m,0,sizeof(f.m));
for(int i=0;i<10;i++) f.m[i][0] = 9-i;
while(cin>>k>>MOD){
for(int i=0;i<10;i++) cin>>aa[i];
if(k<10) printf("%lld\n",k);
else{
// 构建a矩阵
for(int j=0;j<10;j++) a.m[0][j] = aa[j];
for(int i=1;i<10;i++){
for(int j=0;j<10;j++){
if(j+1==i) a.m[i][j] = 1;
else a.m[i][j] = 0;
}
}
mat ans = pow_mat(a,k-9);
ans = ans*f;
printf("%d\n",ans.m[0][0]%MOD);
}
}
return 0;
}
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 ...
- 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 ...
随机推荐
- Linux中的gpio口使用方法
Linux中的IO使用方法 应该是新版本内核才有的方法.请参考:./Documentation/gpio.txt文件 提供的API:驱动需要包含 #include <linux/gpio.h&g ...
- linux下sort对中文排序
http://blog.csdn.net/luoleicn/article/details/6162358 设置: export LC_ALL=C;
- 压力工具代码及epoll使用
服务器编程 P347的压力工具代码不错,对于epoll用的好,可以看.
- HIbernate中openSession和getCurrentSession
这两者的差别网上非常多资源,我这里就copy一下了,然后有点问题的是今天遇到的问题. openSession和getCurrentSession的根本差别在于有没有绑定当前线程,所以,用法有差 ...
- android mvp高速开发框架介绍(dileber使用之图片下载工具)
这几天忙着工作- 今天抽时间又把框架的bug处理了一下--并且把volley的源代码改动了一下 android mvp框架:dileber(https://github.com/dileber/dil ...
- Android开发之控制手机音频
本实例通过MediaPlayer播放一首音乐并通过AudioManager控制手机音频.关于AudioManager的具体解释可參照:Android开发之AudioManager(音频管理器)具体解释 ...
- Android安全攻防战,反编译与混淆技术全然解析(下)
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/50451259 在上一篇文章其中,我们学习了Android程序反编译方面的知识,包括 ...
- mysqli简介
mysqli简介 PHP MySQLi 简介 PHP MySQLi = PHP MySQL Improved! MySQLi 函数允许您访问 MySQL 数据库服务器. 注释:MySQLi 扩展被设计 ...
- EMC存储划分lun过程
下图是EMC存储系统示意图: 若将lun打散重建,需按以下步骤进行: 1. 在Storage Groups上点右键选择Select Luns,在打开的窗口中,将右边Selected Lun项下的lun ...
- 知网下载pdf文件的方法
title: 知网下载pdf文件的方法 toc: false date: 2018-11-02 17:54:43 categories: methods tags: 知网 平时我们使用的是国内版的知网 ...