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 ...
随机推荐
- JavaScript进阶【三】JavaScript面向对象的基础知识复习
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Project Euler 33 Digit cancelling fractions
题意:49/98是一个有趣的分数,因为可能在化简时错误地认为,等式49/98 = 4/8之所以成立,是因为在分数线上下同时抹除了9的缘故.分子分母是两位数且分子小于分母的这种有趣的分数有4个,将这四个 ...
- ELK介绍及搭建 Elasticsearch 分布式集群
上:https://blog.51cto.com/zero01/2079879 下:https://blog.51cto.com/zero01/2082794
- 搞定PHP面试 - 深入了解引用
1. 什么是引用 在 PHP 中引用是指用不同的名字访问同一个变量内容.PHP 中的变量名和变量内容是不一样的, 因此同样的内容可以有不同的名字.最接近的比喻是 Unix 的文件名和文件本身--变量名 ...
- Django入门--模型系统(二):常用查询及表关系的实现
1.常用查询 模型类上的管理器: ** 模型类.objects ** (1)常用一般查询 rs = Student.objects.all() # 查询所有记录,返回Queryset print(rs ...
- php循环练习题
1.通过for循环将数组中值求和.求平均值 <?php $num=[1,2,3,4,5,6,7,8,9]; $sum = 0; for ($i=0,$n=count($num); $i < ...
- jQuery(事件对象)
冒泡与阻止冒泡 阻止默认行为
- 你必须搞清楚的String,StringBuilder,StringBuffer
String,StringBuilder 以及 StringBuffer 这三个类的关系与区别一直是 Java 的经典问题,这次就来讲一下关于这三个类的一些知识 一. 简单对比 String : 字符 ...
- 在Eclipse中搭建Dagger和Dagger2使用环境
眼下Dagger有两个版本号,一个是square的Dagger1.x,另外一个是由google主导与squre联合开发的Dagger2. 本文介绍一下在Eclipse中搭建Dagger和Dagger2 ...
- 非阻塞IO函数
关于效率的优化: