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) + …… + a9 * f(x-10)
同时ai(0<=i<=9) 不是 0 就是 1;
现在给你 ai 的数字,以及k和mod,请你算出 f(x)%mod 的结果是多少
思路:线性递推关系是组合计数中常用的一种递推关系,如果直接利用递推式,需要很长的时间才能计算得出,时间无法承受,但是现在我们已知 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10),那么我们可以根据这个式子构造一个矩阵来解决这得问题
Fn=A×Fn-1 ,其中Fn={f(x-10) ,A={0 1 0 0 0 0 0 0 0 0 0
f(x-9) 0 0 1 0 0 0 0 0 0 0 0
f(x-8) 0 0 0 1 0 0 0 0 0 0 0
........ .................
f(x)} 0 a9 a8 a7 ........... a0}
在利用矩阵快速幂一顿套模板,最后得到矩阵ANS,和ANS中的a0' a1'....a9',我们最后的答案就是a0'*f(9)+a2'*f(8)...a9'*f(0);
代码:
#include <cstdio>
#include <cstring>
#include <iostream> using namespace std; typedef long long ll;
const int N=,M=,P=;
//const int MOD=1000000007;
int MOD;
struct Matrix
{
ll m[N][N];
}; Matrix A;
Matrix I; Matrix multi(Matrix a,Matrix b)
{
Matrix ans;
for(int i=;i<N;i++)
{
for(int j=;j<M;j++)
{
ans.m[i][j]=;
for(int k=;k<P;k++)
{
ans.m[i][j]+=a.m[i][k]*b.m[k][j]%MOD;
}
ans.m[i][j]%=MOD;
}
}
return ans;
} Matrix power(Matrix a,int k)
{
Matrix ans=I,p=a;
while(k)
{
if(k&)
{
ans=multi(ans,p);
}
k>>=;
p=multi(p,p);
}
return ans;
} int main(int argc, char const *argv[])
{
int a[];
ll k;
while(scanf("%lld %lld",&k,&MOD)!=-)
{
for(int i=;i<;i++)
{
scanf("%d",&a[i]);
}
for(int i=;i<N;i++)
{
for(int j=;j<M;j++)
{
I.m[i][j]=;
if(i==j)
{
I.m[i][j]=;
}
}
}
for(int i=;i<N-;i++)
{
for(int j=;j<M;j++)
{
A.m[i][j]=;
if(i+==j)
{
A.m[i][j]=;
}
}
}
A.m[N-][]=;
for(int i=;i<N;i++)
{
A.m[N-][i]=a[-i];
}
Matrix ans = power(A,k-);
ll num=;
for(int i=N-;i>=;i--)
{
num=(num+ans.m[N-][i]*(i-))%MOD;
}
cout<<num<<endl;
}
return ;
}
hdu 1757 A Simple Math Problem (构造矩阵解决递推式问题)的更多相关文章
- 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 (乘法矩阵)
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 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- 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(矩阵快速幂乘法)
Problem Description Lele now is thinking about a simple function f(x). If x < f(x) = x. If x > ...
- 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 (矩阵高速幂)
和这一题构造的矩阵的方法同样. 须要注意的是.题目中a0~a9 与矩阵相乘的顺序. #include <iostream> #include <cstdio> #include ...
- hdu 1757 A Simple Math Problem (矩阵快速幂,简单)
题目 也是和LightOJ 1096 和LightOJ 1065 差不多的简单题目. #include<stdio.h> #include<string.h> #include ...
随机推荐
- devexpress表格控件gridcontrol图片列,按钮列,时间列等特殊列的实现
1.项目中经常会在表格中插入按钮列,图片列,表格列一些非文本的特殊列.如何在devexpress表格控件gridcontrol中实现呢?以下列举一个实现添加图片列,按钮列,时间列,按钮列,开关列的示例 ...
- BZOJ 1076: [SCOI2008]奖励关(概率+dp)
首先嘛,看了这么久概率论真的不错啊。看到就知道怎么写(其实也挺容易的= =) 直接数位dp就行了 CODE: #include<cstdio> #include<cstring> ...
- java学习之路
先来说一说我和it之间的不解之缘.准确来说,我接触it是从大二是我买的第一个手机开始的(国产的,展讯平台,能够运行mrp虚拟机),那时候还没有智能手机,或者说还不够普及,总之就是买不起.一次偶然的机会 ...
- 每天一个linux命令(54)--watch命令
watch是一个非常实用的命,基本所有的Linux发行版都带有这个小工具,如同名字一样,watch可以帮你监测一个命令的运行结果,省的你一遍遍的手动运行,在Linux下,watch是周期性的执行下个程 ...
- nginx内置全局变量
nginx内置全局变量 $args 请求中的参数; $binary_remote_addr 远程地址的二进制表示 $body_bytes_sent 已发送的消 ...
- 使用咪咕云做C站视频直链源
首先我们先百度搜索一下“咪咕云” 点击进入-->用户注册或登录 注册时选择个人用户-->前往邮箱激活-->进入邮箱激活成功后重新登录 登录后在控制台选择“云点播” 即可进行上传视频了 ...
- linux中添加环境变量(python为例)
最近想用Django搭建个人博客,之前学了些python基础语法,准备边学习Django边实战操作.自己有一个阿里云服务器,用的centOS,自带的是python2.7版本,我直接安装了python3 ...
- 解决WebStorm无法连接到Chrome
问题: 点击 中的chrome时,出现了错误,如下: 解决办法: 找到 File>setting>Web Browser 修改为 C:\Program Files (x86)\Google ...
- C中运算符优先级
总体规则: 特殊运算符>单目运算符>双目运算符>三目运算符>赋值运算符>逗号运算符 只有单目运算符是右结合,其余的均为左结合
- java中的对象
对象 --计算机语言中的对象 通常,我们可以从一般事物的三个方面,去认识事物: 一.是什么? 二.为什么? 三.怎么样? 接下来,我们也利用这三个方面的思维,去 ...