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. 2018/09/17《涂抹MySQL》【性能优化及诊断】学习笔记(七)

    读 第十三章<MySQL的性能优化与诊断> 总结 一说性能优化,整个人都像被打了鸡血一样

  2. 《mongoDB》基本操作-创建/更新/删除文档

    一:基本操作 - db; 当前选择的集合(等于数据库名) > db demo -  use db_name; 选择你要操作的集合 > use demo switched to db dem ...

  3. django2.0内置分页

    #导入分页器from django.core.paginator import Paginator 1视图逻辑 #读取数据库 res = Product.objects.all() #建立分页器对象 ...

  4. linux 源码安装 mono

    $ yum install bison gettext glib2 freetype fontconfig libpng libpng-devel libX11 libX11-devel glib2- ...

  5. Python开发【笔记】:aiohttp搭建简易聊天室

    简易聊天室: 1.入口main.py import logging import jinja2 import aiohttp_jinja2 from aiohttp import web from a ...

  6. 这个zsh超级棒

    https://github.com/robbyrussell/oh-my-zsh wget用法: http://man.linuxde.net/wget https://pan.baidu.com/ ...

  7. spring学习(01)之IOC

    spring学习(01)之IOC IOC:控制反转——Spring通过一种称作控制反转(IOC)的技术促进了低耦合.当应用了IOC,一个对象依赖的其它对象会通过被动的方式传递进来,而不是这个对象自己创 ...

  8. vim常用指令整理小结

    启动Vim后,默认是在 Normal 模式下,但是我们有时不知道是在编辑模式还是normal模式,按ESC键就可以返回normal模式.因为所有的命令都需要在Normal模式下使用,所以建议多按几下E ...

  9. git出现warning: LF will be replaced by CRLF的解决方法

    今天ytkah用git上传文件的时候出现了warning: LF will be replaced by CRLF的错误,具体信息如下,这是因为跨平台开发下产生的.由于编辑器的不同或者Windows程 ...

  10. The iOS Simulator deployment target is set to 6.0

    XCODE警告 Showing All Messages :-1: The iOS Simulator deployment target is set to 6.0, but the range o ...