题意:有一个递推式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. P177 test 6-3 UVa536

    //P177 test 6-3 #include<cstdio> #include<cstring> using namespace std; +],s2[+]; int re ...

  2. 一张图看懂 JS 的事件机制

    一.为什么 JavaScript 单线程 假定JavaScript同时有两个线程,一个线程在某个DOM节点上添加内容,另一个线程删除了这个节点,这时浏览器应该以哪个线程为准? 为了避免复杂性, JS ...

  3. Java 内部类详解

    什么 定义在一个类内部的类,称为内部类(累不累),如下: public class A { private int c = 1; public class C { public void test() ...

  4. java读取和写入txt文件

    package com.yinghuo.testDES; import java.io.BufferedReader;import java.io.BufferedWriter;import java ...

  5. 详细领悟ThreadLocal变量

    关于对ThreadLocal变量的理解,我今天查看一下午的博客,自己也写了demo来测试来看自己的理解到底是不是那么回事.从看到博客引出不解,到仔细查看ThreadLocal源码(JDK1.8),我觉 ...

  6. js substr和substring的区别

    在js中substring和substr都是用来截取字符串的,substr函数和substring函数都是用来从某个“母字符串”中提取“子字符串”的函数.但用法有些差别,下面分别介绍但是它们还是有区别 ...

  7. centOS7 mini配置linux服务器(二) 配置IP

    1.登录root用户,输入指令  #ip addr 可以看到除lo外的属于你的网卡配置. 2.输入 #cd /etc/sysconfig/network-scripts/         #vi if ...

  8. 又见Bug

    文章转载自「开发者圆桌」一个关于开发者入门.进阶.踩坑的微信公众号 作为一名开发者,如何解决遇到的问题.异常或Bug,是开发者必须要面对的,尽管问题很多,情况复杂,但还是有方法和技巧可寻的. 问题无非 ...

  9. oracle expdp导出远程数据到本地

    1.本地数据库新建一个用户test,并授予以下基本权限(尽量不要多授权,如本地权限大于远程,会导致导出失败,郁闷!): grant connect to test;grant resource to ...

  10. struts2(一) struts2入门

    首先推荐一本书,虽然我还没看过,但是我以后肯定会看的,<Struts+技术内幕>提取密码:kg6w .现在只是停留在会使用struts2的层次,自己也想继续深入研究,但是感觉自己的知识面还 ...