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. WPF 参数在Page见传递

    void goButton_Click(object sender, RoutedEventArgs e) { this.NavigationService.Navigate(new ContentP ...

  2. 8.1-uC/OS-III多任务应用

    1.app.c: ( 1) .分别为每个任务分配一个OS_TCB. (2). 斥信号量( mutex)是一个内核对象(一个结构体),用于保护共享资源.任务要访问共享资源就必须先获得 mutex. mu ...

  3. sqlserver数据库查询,在数据类型不一致时容易出错

    1. 如此句sql: select SysNo from User_MainInfo where Ouid=@Ouid 在 User_MainInfo表中Ouid是nvarchar类型,但当我们传入的 ...

  4. VS Code 管理 .NET Core解决方案

    本练习要使用Visual studio code完成一个包含多个项目的解决方案,包括类库和Web项目.结合Visual Studio Code和.NET Core CLI,创建项目结构如下: pied ...

  5. 微信小程序分享支持自定义封面图

    微信小程序又发布更新了,刚好昨天支付宝也发布小程序,不能让它抢了风头的节奏.微信小程序主要更新如下:“小程序分享支持自定义封面图,公众号及小程序客服可发送小程序卡片.同时,我们还开放了获取发票抬头,指 ...

  6. Delphi避免重复打开窗体

    取消自动创建窗体 Form1关键代码 implementation uses Unit2; {$R *.dfm} procedure TForm1.Button1Click(Sender: TObje ...

  7. keepalived基础原理

    keepalived介绍 设置初衷是为了高可用ipvs 通过脚本可以实现高可用nginx或者haproxy调度器, 基于vrrp协议完成一个固定的IP可以在集群中不同的节点进行流动.为ipvs集群的各 ...

  8. GPT转MBR怎么转?

    GPT转MBR分区怎么转?现在很多笔记本的硬盘分区都是GPT模式,如果想装XP的话,那只能将GPT磁盘转换成MBR磁盘分区才行.接下来,简单说说如何将GPT分区转成MBR分区! 如果本身电脑有两个硬盘 ...

  9. vue-element分页

    <template> <card-layout :title="L('Users')" :actions="actions" @click=& ...

  10. Scala枚举--Enumeration

    object Color extends Enumeration(2){ val Red,Green,Blue = Value val Yellow = Value("YELLOW" ...