题意:有一个递推式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. windows下读取utf-8文件

    #include <stdio.h> #include <tchar.h> #include <memory> int main() { FILE* fp1 = f ...

  2. Jsp——http status 404 问题

    今天学习Jspapplication内置对象的时候突然碰到了一个问题——http status 404 发生了什么? 提示The requested resource is not available ...

  3. Linux Tomcat安装,Linux配置Tomcat,Linux Tomcat修改内存,Linux tomcat修改端口

    Linux Tomcat安装,Linux配置Tomcat,Linux Tomcat修改内存,Linux tomcat修改端口 >>>>>>>>>& ...

  4. JDK 安装过程

    1.首先是下载jdk:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 2.下载完 ...

  5. 2017-2-22 if语句 if语句的嵌套 以及课堂练习 经典猜拳游戏,闰年判断,输出当前时间的天数等

    (一)if语句 1.格式   if(){ }else if() { } 注意:如果if后面不写花括号,只执行下面第一句 (二)语句1:顺序语句 2:循环语句 3:分支语句 课后练习: 1.猜拳游戏(用 ...

  6. Android消息推送 SDK 集成指南

    使用提示 本文是 Android SDK 标准的集成指南文档. 匹配的 SDK 版本为:r1.8.0及以后版本. 本文随SDK压缩包分发.在你看到本文时,可能当前的版本与本文已经不是很适配.所以建议关 ...

  7. 通过udev创建ASM共享磁盘(RAC)

    OS:RedHat EL6.0 Oracle:   Oracle 11gR2 在Oracle 11gR2,构建RAC时可以通过ASM创建asm disk,但是需要安装asmlib相关软件:对于RedH ...

  8. n的阶乘

    涉及阶乘的都会产生大的数据,此时要变成long或者实在很大要使用BigInteger 题目描述 输入一个整数n,输出n的阶乘 输入描述: 一个整数n(1<=n<=20) 输出描述: n的阶 ...

  9. Java 注解 入门

    这几天在学习Spring3.x,发觉现在许多框架都用上了java注解功能,然后自己就对java注解这方面初步学习了一下. 首先,注解跟注释不是一个意思,也根本不是同一个事物. 注释就是我们平常平常中对 ...

  10. KoaHub平台基于Node.js开发的Koa的get/set session插件代码详情

    koa-session2 Middleware for Koa2 to get/set session use with custom stores such as Redis or mongodb ...