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 (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6621 Accepted Submission(s): 4071
If x < 10 f(x) = x.
If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);
And ai(0<=i<=9) can only be 0 or 1 .
Now, I will give a0 ~ a9 and two positive integers k and m ,and could you help Lele to caculate f(k)%m.
In each case, there will be two lines.
In the first line , there are two positive integers k and m. ( k<2*10^9 , m < 10^5 )
In the second line , there are ten integers represent a0 ~ a9.
1 1 1 1 1 1 1 1 1 1
20 500
1 0 1 0 1 0 1 0 1 0
104
题意概括:
按照题目所给的递推式求解 f(N);
解题思路:
根据递推式构造矩阵乘法;
然后矩阵快速幂解决矩阵乘法;
Ac code:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define LL long long
using namespace std;
const int N = ;
int Mod, K; struct mat
{
int m[][];
}base, tmp, ans; mat muti(mat a, mat b)
{
mat res;
memset(res.m, , sizeof(res.m));
for(int i = ; i <= N; i++)
for(int j = ; j <= N; j++){
if(a.m[i][j]){
for(int k = ; k <= N; k++){
res.m[i][k] = (res.m[i][k] + a.m[i][j] * b.m[j][k])%Mod;
}
}
}
return res;
} mat qpow(mat a, int n)
{
mat res;
memset(res.m, , sizeof(res.m));
for(int i = ; i <= N; i++) res.m[i][i] = 1LL;
while(n){
if(n&){
res = muti(res, a);
}
a = muti(a, a);
n>>=;
}
return res;
} int main()
{ while(~scanf("%d%d", &K, &Mod)){
memset(base.m, , sizeof(base.m));
for(int i = ; i < ; i++){
base.m[i][] = i;
} memset(tmp.m, , sizeof(tmp.m));
for(int i = ; i <= ; i++){
tmp.m[i][i+] = ;
}
for(int i = ; i >= ; i--){
scanf("%d", &tmp.m[][i]); //构造递推关系矩阵
base.m[][] += ((LL)(i-)*tmp.m[][i])%Mod;
}
//see see
// for(int i = 1; i <= 10; i++){
// for(int j = 1; j <= 10; j++){
// printf("%d ", tmp.m[i][j]);
// }
// puts("");
// } if(K <= ){
printf("%d\n", base.m[K][]);
}
else{
tmp = qpow(tmp, K-);
ans = muti(tmp, base);
// //see see
// for(int i = 1; i <= 10; i++){
// for(int j = 1; j <= 10; j++){
// printf("%d ", tmp.m[i][j]);
// }
// puts("");
// }
printf("%d\n", ans.m[][]%Mod);
}
}
return ;
}
HDU 1757 A Simple Math Problem 【矩阵经典7 构造矩阵递推式】的更多相关文章
- HDU 1757 A Simple Math Problem(矩阵)
A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...
- hdu 1757 A Simple Math Problem (乘法矩阵)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- 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 (矩阵乘法)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- hdu 1757 A Simple Math Problem (构造矩阵解决递推式问题)
题意:有一个递推式f(x) 当 x < 10 f(x) = x.当 x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + ...
- HDU 1757 A Simple Math Problem(矩阵高速幂)
题目地址:HDU 1757 最终会构造矩阵了.事实上也不难,仅仅怪自己笨..= =! f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 ...
- 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
题意:当x < 10时, f(x) = x: 当x >= 10 时,f(x) = a0 * f(x-1) + a1 * f(x-2) + + a2 * f(x-3) + …… + a9 ...
- hdu 1757 A Simple Math Problem(矩阵快速幂乘法)
Problem Description Lele now is thinking about a simple function f(x). If x < f(x) = x. If x > ...
随机推荐
- Data Guard 管理原理
##三大优势>Data Guard属于Oracle 自己的产品,其技术成熟完善.稳定可靠>可以随时验证业务数据的有效性>免费产品 Data Guard由主库(PRIMARY DATA ...
- Unity 修改windows窗口的标题
修改windows窗口的标题名称,就是修改下图的东西: 第一种: using UnityEngine; using System; using System.Runtime.InteropServic ...
- Proguard breaking audio file in assets or raw
http://stackoverflow.com/questions/21440572/proguard-breaking-audio-file-in-assets-or-raw Issue: I h ...
- IE Error: '__doPostBack' is undefined 问题解决
突然遇到个很奇怪的BUG,翻页控件,其他浏览器一切正常,IE无法翻页,会提示 '__doPostBack' is undefined 后来搜索发现: [原文發表地址] Bug and Fix: ASP ...
- ListView与ArrayAdapter的搭配使用
在android中,ListView是一种很重要的控件,一般的使用中,常建立一个所需类型的ArrayList,再通过ArrayAdapter把ListView绑定到ArrayList上,通过Array ...
- JavaScript的作用域(Scope)和上下文(Context)
JavaScript对于作用域(Scope)和上下文(Context)的实现是这门语言的一个非常独到的地方,部分归功于其独特的灵活性. 函数可以接收不同的的上下文和作用域.这些概念为JavaScrip ...
- C#异步编程模型
什么是异步编程模型 异步编程模型(Asynchronous Programming Model,简称APM)是C#1.1支持的一种实现异步操作的编程模型,虽然已经比较“古老”了,但是依然可以学习一下的 ...
- Maven入门之简介与安装
一.Maven简介 1.什么是Maven? Maven是一个项目管理工具和集成编译工具,它主要包含如下内容: –一个项目对象模型(Project Object Model), –一组标准集合, –一个 ...
- javaSE练习1——变量和运算符
一.已知a,b均是整型变量,写出将a,b两个变量中的值互换的程序.(知识点:变量和运算符综合应用): package com.test; public class t01 { public stati ...
- 绘图和数据可视化工具包——matplotlib
一.Matplotlib介绍 Matplotlib是一个强大的Python**绘图**和**数据可视化**的工具包. # 安装方法 pip install matplotlib # 引用方法 impo ...