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 ...
随机推荐
- vue组件通信,点击传值,动态传值(父传子,子传父)
转载:https://blog.csdn.net/xr510002594/article/details/83304141 一.父组件传子组件,核心--props 在这里触发 handleClick ...
- strtotime的一个使用问题
我在开发过程中遇到这么这个问题,因为赶进度,没有记下来处理方案,在鸟哥的博客看到原理分析,很到位!平时开发中总是急着处理问题,没有深入分析和记录问题. 1.问题: 今天是2018-07-31 执行代码 ...
- 《黑白团团队》第八次团队作业:Alpha冲刺 第三天
项目 内容 作业课程地址 任课教师首页链接 作业要求 团队项目 填写团队名称 黑白团团队 填写具体目标 认真负责,完成项目 团队项目Github仓库地址链接. 第三天 日期:2019/6/17 成员 ...
- 【codeforces 805B】3-palindrome
[题目链接]:http://codeforces.com/contest/805/problem/B [题意] 让你生成一个只包含a,b,c的字符串; 要求c出现的次数最少,且任意一个 长度为3的子串 ...
- Centos与Ubuntu命令
1.虽然Centos与Ubuntu都是linux的内核,但使用命令还是有所差别 2.如在Centos中跟新插件用的是:yum -y (yum后面有一个空格) 在Ubuntu中跟新插件用的是:apt ...
- [Office]PPT 2013如何设置图片为半透明?
PPT里面似乎无法直接为图片设置透明度属性.下面是一种变通的办法. 1,插入一个和图片大小一致的图形(矩形):2,右键插入的矩形,然后在属性设置里选择“图片填充”,选择以需要的图片填充到该矩形里:3, ...
- HDU 1215
由算术基本定理, 直接使用公式就好 #include <iostream> #include <cstdio> #include <algorithm> #incl ...
- hive join 优化 --小表join大表
1.小.大表 join 在小表和大表进行join时,将小表放在前边,效率会高.hive会将小表进行缓存. 2.mapjoin 使用mapjoin将小表放入内存,在map端和大表逐一匹配.从而省去red ...
- 24岁菜鸟,能一个人撑起App开发吗
"疲惫吾心.怎样躲藏.四处荒芜,怎话忧伤?"临近中秋,看到艾瑞斯的QQ签名,无尽的伤感.这个年仅24的青年.连续3年没有回家了,近期一个月总是失眠,没有家人的陪伴,就连女朋友 ...
- windows下PTAM的编译
前些日子在研究PTAM,以下首先说说PTAM的编译过程,我在XP几WIN7搭配vs2010中均已測试过,都能够执行. 首先下载编译PTAM所必须的库文件.下载地址我会给出 PTAM(PTAM.zip) ...