题意:有一个递推式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 (构造矩阵解决递推式问题)的更多相关文章

  1. 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 ...

  2. hdu 1757 A Simple Math Problem (乘法矩阵)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  3. HDU 1757 A Simple Math Problem (矩阵乘法)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  4. HDU 1757 A Simple Math Problem(矩阵高速幂)

    题目地址:HDU 1757 最终会构造矩阵了.事实上也不难,仅仅怪自己笨..= =! f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 ...

  5. 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) + …… ...

  6. 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 > ...

  7. 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 ...

  8. hdu 1757 A Simple Math Problem (矩阵高速幂)

    和这一题构造的矩阵的方法同样. 须要注意的是.题目中a0~a9 与矩阵相乘的顺序. #include <iostream> #include <cstdio> #include ...

  9. hdu 1757 A Simple Math Problem (矩阵快速幂,简单)

    题目 也是和LightOJ 1096 和LightOJ 1065 差不多的简单题目. #include<stdio.h> #include<string.h> #include ...

随机推荐

  1. oracle系列笔记(1)---查询数据

    查询数据 1. 查询(select .. form ..)    (1)普通查询 select * from employees --代表查询employees表中所有数据 select last_n ...

  2. 基于Blod的ajax进度条下载实现

    普通的浏览器下载 在web开发中,如果要实现下载功能,往往都是使用新开web页面或者是使用iframe的形式.实现起来其实很简单: <a target="_blank" hr ...

  3. AlloyTouch之select选择插件

    原文地址:https://github.com/AlloyTeam/AlloyTouch/wiki/Simple-Select 写在前面 很多情况下,产品希望统一安卓和IOS select交互和样式. ...

  4. http服务搭建

    http服务器搭建 主配置文件在 /etc/httpd/conf/httpd.conf 安装http  yum install httpd -y 启动http服务器  systemctl start ...

  5. SQL Server-聚焦深入理解死锁以及避免死锁建议(三十三)

    前言 终于进入死锁系列,前面也提到过我一直对隔离级别和死锁以及如何避免死锁等问题模棱两可,所以才鼓起了重新学习SQL Server系列的勇气,本节我们来讲讲SQL Server中的死锁,看到许多文章都 ...

  6. Android布局管理详解(1)—— LinearLayout 线性布局

    Android的布局方式共有6种,分别是LinearLayout(线性布局).TableLayout(表格布局).FrameLayout(帧布局).RelativeLayout(相对布局).GridL ...

  7. SpringBoot之旅 -- 定时任务两种(Spring Schedule 与 Quartz 整合 )实现

    相关文章 Spring Boot 相关文章目录 前言 最近在项目中使用到定时任务,之前一直都是使用Quartz 来实现,最近看Spring 基础发现其实Spring 提供 Spring Schedul ...

  8. UI 设计模式 手势识别器

    1> target / action 设计模式 : target ['tɑːgɪt]         1>什么是耦合 : 耦合是衡量一个程序呢写的好坏的标准之一 耦合是衡量模块与模块之间关 ...

  9. Java的Date和Time入门教程

    本文是一篇翻译文章,已取得原作者授权,原文地址是http://tutorials.jenkov.com/java-date-time/index.html Java语言的JDK中关于日期和时间的API ...

  10. scrapy学习笔记

    1.scrapy用哪条命令行重新编辑已有的项目?cd projectname 2.如何在pycharm中开启scrapy?先在终端创建一个项目(即文件夹),再在pycharm中打开.