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 ...
随机推荐
- php设置cookie和删除cookie
设置cookie Example : - set - <?php setcookie( "name", "value", "future_tim ...
- nginx的一些
二.环境准备 三.安装与配置Nginx 四.Nginx之反向代理 五.Nginx之负载均衡 六.Nginx之页面缓存 七.Nginx之URL重写 八.Nginx之读写分离 注,操作系统为 CentOS ...
- 深入了解Python--元组
1. 对原元组进行插入 2. 元组的嵌套使用 3. for循环使用嵌套元组实例 4. 命名元组避免对分片混淆
- 【Codeforces Round #507 (Div. 2, based on Olympiad of Metropolises) A】Palindrome Dance
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] i从1..n/2循环一波. 保证a[i]和a[n-i+1]就好. 如果都是2的话填上min(a,b)*2就好 其他情况跟随非2的. ...
- java+selenium自动化遇到confirm弹窗,出现NoAlertPresentException: no alert open
//操作js的confirm弹窗,bool控制是否点击确定,true为点击确定,false为点击取消 public static void OperaterJSOfConfirm(WebDriver ...
- (7)JPA - Hibernate【从零开始学Spring Boot】
在说具体如何在spring boot 使用Hibernate前,先抛装引玉些知识点?什么是JPA呢? JPA全称Java Persistence API.JPA通过JDK 5.0注解或XML描述对象- ...
- Unhandled 'error' event
events.js: throw er; // Unhandled 'error' event ^ Error: listen EADDRINUSE at errnoException (net.js ...
- linux网络监控脚本
http://www.51testing.com/html/92/77492-828434.html
- Android处理错误json数据
此前一直都没遇到也一直相信服务端返回的json数据基本是正确的,直到我们的android端一直崩溃,并在友盟上查到一直报如下的错误: com.google.gson.JsonSyntaxExcepti ...
- 建议53:用状态模式美化代码,关于python-state工具包的理解
在<编写高质量代码:改善python程序的91个建议>的建议53:用状态模式美化代码小节中,介绍了状态模式例如以下:就是当一个对象的内在状态改变时,同意改变其行为,但这个对象看起来 ...