Lele now is thinking about a simple function f(x).

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.

构造一个矩阵

{

a0 a1 a2 a3 a4 a5 a6 a7 a8 a9

1  0   0   0   0  0   0  0   0   0

0  1   0   0   0  0   0  0   0   0

0  0   1   0   0  0   0  0   0   0

0  0   0   1   0  0   0  0   0   0

0  0   0   0   1  0   0  0   0   0

0  0   0   0   0  1   0  0   0   0

0  0   0   0   0  0   1  0   0   0

0  0   0   0   0  0   0  1   0   0

0  0   0   0   0  0   0  0   1   0

}得解

#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <math.h>
using namespace std;
typedef long long LL;
const int maxn=;
int MOD;
struct Matrix
{
LL m[maxn][maxn];
Matrix mult(Matrix &rhs)
{
Matrix ans;
for(int i=; i<; i++)
for(int j=; j<; j++)
{
ans.m[i][j]=;
for(int k=; k<; k++)
ans.m[i][j]=(ans.m[i][j]+(m[i][k]*rhs.m[k][j])%MOD )%MOD; }
return ans;
}
};
int a[];
void powk(int n)
{
Matrix A,ans;
memset(ans.m,,sizeof(ans.m));
memset(A.m,,sizeof(A.m));
for(int i=; i<; i++){
ans.m[i][i]=;
A.m[][i]=a[i];
A.m[i+][i]=;
}
while(n)
{
if(n&)ans=ans.mult(A);
n>>=;
A=A.mult(A);
}
LL AS=;
for(LL i=; i<; i++){
AS=(AS+(ans.m[][i]*(-i+))%MOD )%MOD;
}
printf("%I64d\n",AS);
}
int main()
{
int k;
while(scanf("%d%d",&k,&MOD)==)
{
for(int i=; i<; i++)scanf("%d",&a[i]);
if(k<){
printf("%d\n",k%MOD); continue;
}
powk(k-);
}
return ;
}

hdu1757 构造矩阵的更多相关文章

  1. POJ 3233 Matrix Power Series(构造矩阵求等比)

    Description Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + … + Ak. ...

  2. Number Sequence(HDU 1005 构造矩阵 )

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  3. hdu 5015 233 Matrix(构造矩阵)

    http://acm.hdu.edu.cn/showproblem.php?pid=5015 由于是个二维的递推式,当时没有想到能够这样构造矩阵.从列上看,当前这一列都是由前一列递推得到.依据这一点来 ...

  4. [数学-构造矩阵]NEFU 1113

    依据题意.我已经推导出tn的公式.ti=ti.a+ti.b,ti.a=5*t(i-1).a+4*t(i-1).b,ti.b=t(i-1).a+t(i-1).b 然而以下居然不能继续推到sn的公式!!! ...

  5. poj 3735 Training little cats(构造矩阵)

    http://poj.org/problem?id=3735 大致题意: 有n仅仅猫,開始时每仅仅猫有花生0颗,现有一组操作,由以下三个中的k个操作组成: 1. g i 给i仅仅猫一颗花生米 2. e ...

  6. HDU 3306 Another kind of Fibonacci ---构造矩阵***

    Another kind of Fibonacci Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  7. 构造矩阵解决这个问题 【nyoj299 Matrix Power Series】

    矩阵的又一个新使用方法,构造矩阵进行高速幂. 比方拿 nyoj299 Matrix Power Series 来说 给出这样一个递推式: S = A + A2 + A3 + - + Ak. 让你求s. ...

  8. UVa 11149 Power of Matrix (矩阵快速幂,倍增法或构造矩阵)

    题意:求A + A^2 + A^3 + ... + A^m. 析:主要是两种方式,第一种是倍增法,把A + A^2 + A^3 + ... + A^m,拆成两部分,一部分是(E + A^(m/2))( ...

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

随机推荐

  1. 《HTTP - http报文》

    还时推荐一首歌 - 那吾克热<纸飞机> 有没有突然想要个孩子的冲动,哈哈. 读 第三章<HTTP报文内的HTTP信息> 总结 1:用于HTTP协议交互叫做HTTP报文,请求端( ...

  2. java JDBC (六) org.apache.commons.dbutils 增删改

    dbutils是apache封装了JDBC的工具类,比mysql-connector更方便些 下载地址:http://commons.apache.org/proper/commons-dbutils ...

  3. 数据库使用:sql server/mysql/sqlite

    本科学过sql server数据库,上研刚开始在做研究时自己想点子自己做,为了简便使用了论文中看到的一个简易数据库sqlite存储使用数据.后来随着数据量的增长,以及数据处理的需求sqlite速度明显 ...

  4. 贪吃蛇java版

    主要的蛇的类 import java.awt.Color; import java.awt.Graphics; import java.awt.HeadlessException; import ja ...

  5. 程序------>算法

    一.算法: 1.算法的定义:   算法是解决特定问题求解步骤的描述,在计算机中表现为指令的有序序列,并且每条指令表示一个或多个操作.即算法是描述解决问题的方法.(对于给定的问题是可以有多种 算法进行解 ...

  6. 漫画:什么是HashMap

    漫画:什么是HashMap 参考: HashMap源码解析 美团技术团队:Java 8系列之重新认识HashMap

  7. SpringBoot-区分不同环境配置文件

    spring.profiles.active=pre application-dev.properties:开发环境 application-test.properties:测试环境 applicat ...

  8. MySQL Sandbox安装使用

    前言 经常看见新同学对于安装MySQL十分惧怕,这是因为不熟悉的缘故,确实源码编译比较麻烦,缺少各种依赖包也够折腾的.当然还有预编译包,但是对于新同学来说好像都挺麻烦的.当然有yum安装,但是我们需要 ...

  9. php程序猿面试分享

    面试总结 今天去了北京著名IT公司进行PHP程序猿的面试.这是人生第一次么,怎么不紧张?我是不是有病.不是.这叫自信呵. 首先是做一些笔试题. 1.mysql数据库索引使用的数据结构?这样做的优点是? ...

  10. 20170720 Celery 异步任务处理到Sql Server 发生死锁

    -- 1.  异常提示情况如下: 需要解决为什么引起死锁 -- 叹气 原因: 在使用Celery 启用了 配置参数 CELERYD_CONCURRENCY = 10  表示开了10块线程池. 有好处, ...