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. 数据库使用:sql server/mysql/sqlite

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

  2. 关于flexjson将json转为javabean的使用

    关于flexjson将json转为javabean的使用 import java.sql.Timestamp; import java.util.Date; import flexjson.JSOND ...

  3. 5分钟简述Spring中的DI与AOP

    Spring的两个核心特性: 依赖注入(dependency injection,DI) 面向切面编程(aspect oriented programming,AOP) 依赖注入(dependency ...

  4. SpringBoot-整合@transactional注解

    springboot默认集成事务,只主要在方法上加上@Transactional即可

  5. 让网站不去请求favicon.ico图标

    让网站不去请求favicon.ico图标 favicon.ico 图标用于收藏夹图标和浏览器标签上的显示,如果不设置,浏览器会请求网站根目录的这个图标,如果网站根目录也没有这图标会产生 404.出于优 ...

  6. webuploader.min.js 简单例子

    一个百度开发的开源框架 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html& ...

  7. centOS和windows7双系统下重装windows后恢复centOS引导

    电脑原本是windows7和centOS双系统,后来重装windows系统后,发现没有centOS引导,于是重新恢复centOS引导. 1.官网下载centos系统镜像CentOS-7-x86_64- ...

  8. bcolz的新操作

    1.直接修改 eg:把data.bcolz文件中A列为0的数据填充为1000. data = bcolz.open("data.bcolz", "a") #以& ...

  9. configure.in详解

    configure.in文件里基本的内容就是一系列的m4宏,在运行时根据传递给它们的参数,定义的宏就会扩展为shell的脚本代码段.也可以手工书写shell代码.不过我们就不说这个了,要想完全的理解c ...

  10. save

    docker-compose-orderer.yaml # Copyright IBM Corp. All Rights Reserved. # # SPDX-License-Identifier: ...